When running this code in excel I am getting the following error:
Run-time error '1004':
Method 'UpdateLink' of object '_Workbook' failed
What's wrong? Here is the code I'm using:
Sub UpdateSelectedLinks()
Dim aLinks
Dim cLink As String
Dim i As Integer
Application.ScreenUpdating = False
aLinks = ActiveWorkbook.LinkSources(xlExcelLinks)
If Not IsEmpty(aLinks) Then
For i = 1 To UBound(aLinks)
cLink = Split(aLinks(i), "\")(UBound(Split(aLinks(i), "\")))
If cLink <> "MasterList.xls" Then
ActiveWorkbook.UpdateLink Name:=aLinks(i), Type:=xlExcelLinks
End If
Next i
End If
Application.ScreenUpdating = True
End Sub
When I choose to debug the row that's hightlighted is "
ActiveWorkbook.UpdateLink Name:=aLinks(i), Type:=xlExcelLinks"
HELP!!!
Thanks!
macropod said:
Hi Chris,
Actually, on relection, changing these two lines:
Workbooks.Open aLinks(i)
Workbooks(cLink).Close SaveChanges:=False
to:
ActiveWorkbook.UpdateLink Name:=aLinks(i), Type:=xlExcelLinks
should work just as well, if not better, as it avoids all the file opening/closing.
--
Cheers
macropod
[MVP - Microsoft Word]
Thank you so much!!! One question, if I'm telling excel to update all the
links but one, why do I need to open the other workbooks?
:
Hi Chris,
You can do all the temporary opening (then closing) with vba. For example:
Sub UpdateSelectedLinks()
Dim aLinks
Dim cLink As String
Dim i As Integer
Application.ScreenUpdating = False
aLinks = ActiveWorkbook.LinkSources(xlExcelLinks)
If Not IsEmpty(aLinks) Then
For i = 1 To UBound(aLinks)
cLink = Split(aLinks(i), "\")(UBound(Split(aLinks(i), "\")))
If cLink <> "IgnoreIt.xls" Then
Workbooks.Open aLinks(i)
Workbooks(cLink).Close SaveChanges:=False
End If
Next i
End If
Application.ScreenUpdating = True
End Sub
where 'IgnoreIt.xls' is the name of the workbook you don't want to update the links to.
--
Cheers
macropod
[MVP - Microsoft Word]
Thanks for the suggestion but that's not in option in my case. I need to
write something in VBA that would update each source documents links - I've
already disabled the update links pop up from appearing and have it set to
not update. Any other suggestions?
:
Hi Chris,
If you disable the links, then temporarily open each of the workbooks for which you want the links to update, that should
address
the problem. Note: AFAIK, you can't prevent links to an open workbook from updating.
--
Cheers
macropod
[MVP - Microsoft Word]
I have a document that has links to 6 different source documents. I only want
to have 5 of them updated (the last one is updated by being "pushed down"
from that document when it's changed). Is there a way to do this? Please
help!!! I've been looking for an answer to this question for over 2 days
now...
Thanks!