Solved

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

Posted on 2012-03-29
3
323 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
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…

738 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