Solved

Changing Field Links in a Word Document from one excel file to another

Posted on 2012-03-29
3
320 Views
Last Modified: 2012-03-29
I have a series of word documents that have links to a spreadsheet throughout the body and in the header and footer.

I am trying to change the links from one spreadsheet to a new spreadsheet.

The following code works great updating the links in the body of the document but does not change the links in the header and footer.

I thought the command "For Each oStory In ActiveDocument.StoryRanges" would cause the code to run in each section, but it does not.

-----------------------------------------------------------------------

Sub a_UpdateChangeLinkSource()
    Dim dlgSelectFile As FileDialog 'FileDialog object
    Dim thisField As Field
    Dim selectedFile As Variant 'must be Variant to contain filepath of selected item
    Dim newFile As Variant
    Dim fieldCount As Integer
    Dim oStory As Range

    'create FileDialog object as File Picker dialog box
    Set dlgSelectFile = Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
   
    With dlgSelectFile
    'use Show method to display File Picker dialog box and return user's action
        If .Show = -1 Then
        'step through each string in the FileDialogSelectedItems collection
            For Each oStory In ActiveDocument.StoryRanges
                For Each selectedFile In .SelectedItems
                    newFile = selectedFile 'gets new filepath
                Next selectedFile
            Next oStory
        Else 'user clicked cancel
        End If
    End With
    Set dlgSelectFile = Nothing
   
    'update fields
    fieldCount = ActiveDocument.Fields.Count
           
    For Each oStory In ActiveDocument.StoryRanges
        For x = 1 To fieldCount
            ActiveDocument.Fields(x).LinkFormat.SourceFullName = newFile
        Next x
    Next oStory
End Sub
000000---Cover-Sheet-Vol-1-of-2.doc
0
Comment
Question by:rogerdjr
  • 2
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 total points
ID: 37783809
Stepping through the story range types only finds the first range in each story type. See this code to update all the fields in a document
Private Sub UpdateAllFields()
    Dim oRange As Range
    Dim oRange2 As Range
    ' Go through all story ranges in the document, including shapes,headers & footers.
    For Each oRange In ActiveDocument.StoryRanges
        Set oRange2 = oRange 'set oRange2 to first range in story
        Do
            oRange2.Fields.Update
            Set oRange2 = oRange2.NextStoryRange
        Loop Until oRange2 Is Nothing
    Next oRange
End Sub

Open in new window

0
 

Author Comment

by:rogerdjr
ID: 37784668
Thanks

the final code looks like this:

Sub a_UpdateChangeLinkSource()
    Dim oStory As Range
    Dim oField As Field
     
    Dim dlgSelectFile As FileDialog 'FileDialog object
    Dim thisField As Field
    Dim selectedFile As Variant 'must be Variant to contain filepath of selected item
    Dim newFile As Variant
    Dim fieldCount As Integer, FldInx As Integer
     
    'create FileDialog object as File Picker dialog box
    Set dlgSelectFile = Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
   
    With dlgSelectFile
    'use Show method to display File Picker dialog box and return user's action
        If .Show = -1 Then
        'step through each string in the FileDialogSelectedItems collection
            For Each selectedFile In .SelectedItems
                newFile = selectedFile 'gets new filepath
            Next selectedFile
        Else 'user clicked cancel
        End If
    End With
    Set dlgSelectFile = Nothing
   
    For Each oStory In ActiveDocument.StoryRanges
            fieldCount = oStory.Fields.Count
            FldInx = 1
            While FldInx < fieldCount
                On Error Resume Next
                    oStory.Fields(FldInx).LinkFormat.SourceFullName = newFile
                On Error GoTo 0
                FldInx = FldInx + 1
            Wend
    Next oStory
End Sub
0
 

Author Closing Comment

by:rogerdjr
ID: 37784670
Thanks the final code that works looks like this:

Sub a_UpdateChangeLinkSource()
    Dim oStory As Range
    Dim oField As Field
     
    Dim dlgSelectFile As FileDialog 'FileDialog object
    Dim thisField As Field
    Dim selectedFile As Variant 'must be Variant to contain filepath of selected item
    Dim newFile As Variant
    Dim fieldCount As Integer, FldInx As Integer
     
    'create FileDialog object as File Picker dialog box
    Set dlgSelectFile = Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
   
    With dlgSelectFile
    'use Show method to display File Picker dialog box and return user's action
        If .Show = -1 Then
        'step through each string in the FileDialogSelectedItems collection
            For Each selectedFile In .SelectedItems
                newFile = selectedFile 'gets new filepath
            Next selectedFile
        Else 'user clicked cancel
        End If
    End With
    Set dlgSelectFile = Nothing
   
    For Each oStory In ActiveDocument.StoryRanges
            fieldCount = oStory.Fields.Count
            FldInx = 1
            While FldInx < fieldCount
                On Error Resume Next
                    oStory.Fields(FldInx).LinkFormat.SourceFullName = newFile
                On Error GoTo 0
                FldInx = FldInx + 1
            Wend
    Next oStory
End Sub
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had the experience that you had to follow 10 steps over and over again every time when you need to nicely forward an important email to your manager? Fear no more! With the help of the Quick Steps feature in Outlook 2010, your old chor…
This article will show you how to use shortcut menus in the Access run-time environment.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question