View Single Post
 
Old 11-30-2014, 04:09 PM
ptmuldoon ptmuldoon is offline Windows 7 64bit Office 2013
Advanced Beginner
 
Join Date: Sep 2014
Posts: 93
ptmuldoon is on a distinguished road
Default

Macropod.

I'm going to play more with the code you just posted, but I found an old post (by you actually ) that used code.text in the replace.
your old post
http://www.wordbanter.com/showthread.php?t=76379

So I am having success with the below code currently.

The below will prompt for the new File and replace the FilePath and FileName Separately. It's pretty 'quick', replacing about 70 links in a few seconds.

Code:
Sub ChangeFileLinks()
    Dim f As Object
    Dim i, x, fieldCount As Long
    Dim OldPath, OldFile As String
    Dim NewPath, NewFile As String
    
    Set f = Application.FileDialog(3)

    f.AllowMultiSelect = False

    If f.Show Then
        For i = 1 To f.SelectedItems.Count
            'Get the File Path Only
            NewPath = f.InitialFileName
            NewPath = Replace(NewPath, "\", "\\")
            MsgBox "The New File Path is: " & NewPath
            'Get the FileName only.  Uses Public FileName Function Below
            MsgBox "The FileName Only is: " & Filename(f.SelectedItems(i))
            NewFile = Filename(f.SelectedItems(i))
        Next
    End If
    
    With ActiveDocument
        fieldCount = .Fields.Count
        For x = 1 To fieldCount
            With .Fields(x)
                'Debug.Print .Type
                If .Type = 56 Then
                    'Get The Existing FilePath and File Name from the Link Sources
                    OldPath = .LinkFormat.SourcePath & "\"
                    OldPath = Replace(OldPath, "\", "\\")
                    'MsgBox "The Existing FilePath is: " & OldPath
                    
                    OldFile = .LinkFormat.SourceName
                    'MsgBox "The Existing File Name is: " & .LinkFormat.SourceName
                    
                    'Replace the FilePath
                    ' Replace the link to the external file
                    .Code.Text = Replace(.Code.Text, OldPath, NewPath)
                    '.LinkFormat.SourceFullName = NewPath
                    
                    'Replace the FileName
                    '.LinkFormat.SourceName = NewFile
                    .Code.Text = Replace(.Code.Text, OldFile, NewFile)
                    .Update
                End If
            End With
        Next x
    End With
    
End Sub
Reply With Quote