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: