Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2012-03-29
3
Medium Priority
?
351 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 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

Microsoft has changed the look and feel of Azure AD and Microsoft account sign-in pages so that you will have a more unified look and feel when moving between the two interfaces.
With its various features, Office 365 can not only help you with your day-to-day business tasks, it can also do wonders for your marketing campaign.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
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…

636 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