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
About the author
Hey, I’m Mark, and I run Excel Off The Grid.
My parents tell me that at the age of 7 I declared I was going to become a qualified accountant. I was either psychic or had no imagination, as that is exactly what happened. However, it wasn't until I was 35 that my journey really began.
In 2015, I started a new job, for which I was regularly working after 10pm. As a result, I rarely saw my children during the week. So, I started searching for the secrets to automating Excel. I discovered that by building a small number of simple tools, I could combine them together in different ways to automate nearly all my regular tasks. This meant I could work less hours (and I got pay raises!). Today, I teach these techniques to other professionals in our training program so they too can spend less time at work (and more time with their children and doing the things they love).
Do you need help adapting this post to your needs?
I'm guessing the examples in this post don't exactly match your situation. We all use Excel differently, so it's impossible to write a post that will meet everybody's needs. By taking the time to understand the techniques and principles in this post (and elsewhere on this site), you should be able to adapt it to your needs.
But, if you're still struggling you should:
- Read other blogs, or watch YouTube videos on the same topic. You will benefit much more by discovering your own solutions.
- Ask the 'Excel Ninja' in your office. It's amazing what things other people know.
- Ask a question in a forum like Mr Excel, or the Microsoft Answers Community. Remember, the people on these forums are generally giving their time for free. So take care to craft your question, make sure it's clear and concise. List all the things you've tried, and provide screenshots, code segments and example workbooks.
- Use Excel Rescue, who are my consultancy partner. They help by providing solutions to smaller Excel problems.
Don't go yet, there is plenty more to learn on Excel Off The Grid. Check out the latest posts:
11 thoughts on “Edit links in Word using VBA”
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.
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.
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