0

I have a workbook within which there are links to number of other workbooks. Ideally I'd like to code: When I open the workbook containing the links, will update the links, but only if the workbook that is being linked to is not open (either on my PC or another user's PC on the network, that can access it). This is because:

  • a) if the workbook is already open the link should update anyway,
  • b) updating links to workbooks that are already open seems to cause errors.

I hope I'm making sense, please tell me if I'm not. Thanks.

2
  • 1
    Have you written some code to start with? Commented Jan 30, 2015 at 11:23
  • How do you want to figure out the path of the linked workbooks? Commented Jan 30, 2015 at 11:24

1 Answer 1

1

Try this

Sub UpdateLinks()
    Dim v As Variant, i As Long
    v = ThisWorkbook.LinkSources(XlLink.xlExcelLinks)
    For i = 1 To UBound(v)
        If Not FileInUse(v(i)) Then
            Workbooks.Open (v(i))
        End If
    Next i
    ActiveWorkbook.UpdateLink Name:=ActiveWorkbook.LinkSources
End Sub

Public Function FileInUse(sFileName) As Boolean 'Checks if a workbook is open
    On Error Resume Next
    Open sFileName For Binary Access Read Lock Read As #1
    Close #1
    FileInUse = IIf(Err.Number > 0, True, False)
    On Error GoTo 0
End Function
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.