Last week we looked at linking Excel directly to Word documents. This can create significant time saving as the Word document updates automatically whenever the Excel file changes. The problem comes when we want to change the file path of the linked document, as it is necessary to change the links one by one. But, we can use VBA to automate this process.
The VBA code below has been included twice, once for using within Excel and once for using within Word.
Here are a few notes for using the code:
- The Excel code has been written using Late Binding. Which means it should run by itself; you do not need to create references to the Word Object library.
- When changing file paths remember to end the file path with a slash “\”
Edit the linked documents using Word VBA
To open the Visual Basic editor in Word press Alt + F11. Right-click on ThisDocument for your document and select Insert -> Module.
Copy the code below into the code window.
For this code work you just need to update the oldFilePath and newFilePath variables to meet your requirements.
Sub UpdateWordLinks() Dim newFilePath As String Dim oldFilePath As String Dim sourceFileName As String Dim newFileName As String Dim wrdDocument As Document Dim i As Integer 'The old file path as a string (the text to be replaced) oldFilePath = "String of\File Path\To Be Replaced\Excel File.xlsx" 'The new file path as a string (the text to replace with) newFilePath = "String of\New File Path\Excel File 2.xlsx" 'Set the variable to the Word Document Set wrdDocument = ThisDocument 'Use Replace to change the oldFilePath to the newFilePath on the Field code For i = 1 To wrdDocument.Fields.Count wrdDocument.Fields(i).LinkFormat.SourceFullName _ = Replace(wrdDocument.Fields(i).LinkFormat.SourceFullName, _ oldFilePath, newFilePath) Next i 'Update the links wrdDocument.Fields.Update End Sub
Editing the linked documents from Excel using VBA
You may decide that Excel is a better location to keep the code for editing links. In Excel, press ALT + F11 to open the Excel Visual Basic Editor. Click Insert -> Module. Copy the code below into the new Module.
For this code work you just need to update the sourceFileName, oldFilePath and newFilePath variables to meet your requirements.
Sub UpdateWordLinks() Dim oldFilePath As String Dim newFilePath As String Dim sourceFileName As String Dim newFileName As String Dim wrdApp As Object Dim wrdDocument As Object Dim i As Integer 'The file name and path of the file to update sourceFileName = "C:\Users\marks\Documents\Test Word Link 1.docx" 'The old file path as a string (the text to be replaced) oldFilePath = "C:\Users\marks\Documents\Test Word Link.xlsx" 'The new file path as a string (the text to replace with) newFilePath = "C:\Users\marks\Documents\P1\Test Word Link P1.xlsx" 'Set the variable to the Word Application Set wrdApp = CreateObject("Word.Application") 'Make the Word application visible wrdApp.Visible = True 'Set the variable to the Word Document Set wrdDocument = wrdApp.Documents.Open(sourceFileName) 'Use Replace to change the oldFilePath to the newFilePath on the Field code For i = 1 To wrdDocument.Fields.Count wrdDocument.Fields(i).LinkFormat.SourceFullName _ = Replace(wrdDocument.Fields(i).LinkFormat.SourceFullName, _ oldFilePath, newFilePath) Next i 'Update the links wrdDocument.Fields.Update 'Save, close and quit the application wrdDocument.Save wrdDocument.Close wrdApp.Quit 'Release the memory Set wrdApp = Nothing Set wrdDocument = Nothing End Sub
Discover how you can automate your work with our Excel courses and tools.
Excel Academy
The complete program for saving time by automating Excel.
Excel Automation Secrets
Discover the 7-step framework for automating Excel.
Office Scripts: Automate Excel Everywhere
Start using Office Scripts and Power Automate to automate Excel in new ways.
I don’t understand this whole piece. Why would you write code to do this? You can change the linked file in Word itself. Click on File, lower right side under “Related Documents” is “Edit Links to Files”. You click on that to change the file to link to.
Hi Ron,
If you’ve got one or two links, then you’ve got a point.
What if you’ve got 60 links?
What if the links are to different documents?
What if somebody else controls the Excel files and changes the name of the tabs, or saves all the files with a different file path?
Sure, you could go through and update it manually, but that can be time-consuming. I’ve been in these situations, and the VBA code significantly faster.
I am still not sure you are understanding.
What if you have 60 links?
Answer, if they are all to the same document, then you have just one change.
When you state: “The problem comes when we want to change the file path of the linked document, as it is necessary to change the links one by one. ”
You are talking about a single document, and saying that we would need to go through and change the links one by one for each linked field, (which simply isn’t true), and then you give code to change each of these fields with VBA.
I appreciate your example, however, and it helps to understand the code. Just the original statement is wrong or misleading at least to my comprehension, if you meant something else.
In my case, I am trying to decide if it is better to use Bookmarks in Word and Range Name in Excel, and then VBA code to change the Word Bookmark values to the matching Excel Range Name, or use the linked capability to do the updates.
I am thinking, the best choice is for the user to give the Excel fields a Range Name, then copy special to the word document. If I use Bookmarks in Word, if they write over that, the Bookmark is gone. If we use Linked fields, then the link remains even if they write over it. Perhaps advantage with Bookmarks is I can insert fields with calculations adding Bookmark1 with 2, etc. I am not seeing in the fields a formula to add linked values.
Hi Ron,
I thought Edit Links only let you change one at a time. It’s possible to display field codes, then do a find/replace, to update multiple links at the same time. I will review the post and update accordingly.
In terms of what you’re trying to achieve, I currently use linked fields for a similar process. Though I’ve found the success depends on the skill of the users, it’s amazing how many different ways they can break the links without even realizing 🙂
Is it possible to adapt the Excel code so that the links to the document and the Excel files update are held in cell references within the workbook (i.e. the user can update them without having to venture into the VBA code)?
Yes, that’s possible just change the following lines to reference a specific cells:
I would like to apply this code, but my excel diagramms are not found.
The counter: wrdDocument.Fields.Count is 0.
Are the diagrams linked to Excel – what happens when you view the field codes by pressing Shift+F9?
Hi, thanks for the code but I am getting error 91 message running the code as is. It does not seem to recognize the word object. The code runs into an error at the For I=1 statement.
Very crafty piece of code. Would there be a way to have this be able to update links that are in header’s and text box’s as well?
Hi, thanks a lot for this code it is very useful. The only thing I’m missing to make it work for my purpose is to be able to activate de “Preserve formatting after update” option because otherwise the sizes of my tables get modified when I change the source.
Thanks a lot and BR
Oscar