View Single Post
 
Old 07-03-2015, 02:02 AM
Fed77 Fed77 is offline Windows 7 64bit Office 2010 64bit
Novice
 
Join Date: Jul 2015
Location: Venice
Posts: 5
Fed77 is on a distinguished road
Default Changing Field Links in a Word Document from one excel file to another

Thanks in advance,
I have a word document that have links to an excel spreadsheet throughout the body and in the header and footer.
I need to update the source to all the links by using a macro in word VBA.
The following macro should be able to offer a dialog box to user then they select file and it replaces current source in all links in the word doc, but it is not working in the header and footer.
Someone could help me to figure out what is wrong?
__________________________________________________ _______________

Code:
Sub Update_link()
'
' Update_link Macro
'
Dim oStory As Range
Dim oField As Field
Dim dlgSelectFile As FileDialog 'Filedialog Object
Dim thisFiled As Field
Dim selectedFile As Variant ' contain filepath of selected item
Dim newfile As Variant
Dim fieldCount As Integer, FldInx As Integer
'
'create FileDialog object as File picker Dialog boz
'
Set dlgSelectFile = Application.FileDialog(FileDialogType:=msoFileDialogFilePicker)
'
With dlgSelectFile
  ' use Show method to dispaly File Picker dialog box and return user's action
  If .Show = -1 Then
    'step throug 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
'
'update fields
'
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
__________________________________________________ ______________

Last edited by macropod; 07-06-2015 at 01:41 PM. Reason: Added code tags & formatting
Reply With Quote