Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Outlook Free & Paid Tools
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

867 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

22 Experts available now in Live!

Get 1:1 Help Now