Solved

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

Posted on 2012-03-29
3
299 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Problem: You created a new custom form in Outlook for your contacts (added fields, deleted fields, changed the layout of fields, whatever) and made it the default form for contacts. The good news is that all new contacts will utilize the new form. T…
In this article we discuss how to recover the missing Outlook 2011 for Mac data like Emails and Contacts manually.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

758 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now