Edit links in Word using VBA

Edit link in Word using VBA

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 “\”

Generate accurate VBA code in seconds with AutoMacro

AutoMacroExample

AutoMacro is a powerful VBA code generator that comes loaded with an extensive code library and many other time-saving tools and utilities.

Whether you’re an experienced coder looking to save time, or a newbie just trying to get things to work, AutoMacro is the tool for you.

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.

Word VBA insert Module

Copy the code below into the code window.

Word VBA Editor With Code

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

Don’t forget:

If you’ve found this post useful, or if you have a better approach, then please leave a comment below.

Do you need help adapting this to your needs?

I’m guessing the examples in this post didn’t exactly meet 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:

  1. Read other blogs, or watch YouTube videos on the same topic.  You will benefit much more by discovering your own solutions.
  2. Ask the ‘Excel Ninja’ in your office.  It’s amazing what things other people know.
  3. 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.
  4. Use Excel Rescue, who are my consultancy partner.   They help by providing solutions to smaller Excel problems.

What next?
Don’t go yet, there is plenty more to learn on Excel Off The Grid.  Check out the latest posts:

10 thoughts on “Edit links in Word using VBA

  1. Ron Bonnell says:

    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.

  2. Excel Off The Grid says:

    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.

  3. Ron Bonnell says:

    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.

    • Excel Off The Grid says:

      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 🙂

  4. Smith says:

    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)?

    • Excel Off The Grid says:

      Yes, that’s possible just change the following lines to reference a specific cells:

      sourceFileName = Sheets("Sheet1").Range("A1")
      oldFilePath = Sheets("Sheet1").Range("A2")
      newFilePath = Sheets("Sheet1").Range("A3")
  5. Matthias Pospiech says:

    I would like to apply this code, but my excel diagramms are not found.
    The counter: wrdDocument.Fields.Count is 0.

    • Excel Off The Grid says:

      Are the diagrams linked to Excel – what happens when you view the field codes by pressing Shift+F9?

      • Dominic Fournier says:

        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.

  6. Matthew Lazarus says:

    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?

Leave a Reply

Your email address will not be published. Required fields are marked *