Change all links' sources in a Word document - Misplacement of Ranges

后端 未结 2 874
名媛妹妹
名媛妹妹 2021-01-11 17:56

I work on this code to change the sources of all linked Fields/Charts/... in Word templates to the workbook it is launched from.

I had usual

相关标签:
2条回答
  • 2021-01-11 18:37

    I think using the hyperlinks collection is the key to your solution - unless you have a specific reason not to. Links from a Word document to an Excel workbook are external links so should all be listed in the Hyperlinks collection (regardless of whether they are text links or InlineShapes that are linked).

    Here's my code that may be of some help. For simplicity I've hard coded the Word document since that's not an issue for you:

    Sub change_Templ_Args()
        WbkFullname = ActiveWorkbook.FullName
    
        'Alternatively...
        'WbkFullname = "C:\temp\myworkbook.xlsx"
        'Application.Workbooks.Open Filename:=WbkFullname
    
        'Get Document filename string
        MyWordDoc = "C\Temp\mysample.docx"
    
        Set oW = CreateObject("Word.Application")
        oW.Documents.Open Filename:=MyWordDoc 
        Set oDoc = oW.ActiveDocument
    
        'Reset Hyperlinks
        For Each HypLnk In oDoc.Hyperlinks
            HypLnk.Address = WbkFullname
        Next
    
    End Sub
    

    If you really need to use Fields and InlineShapes try this code. I've used variants in For loop and added a check for wdLinkTypeReference for fields that are Table of Contents or Cross Reference fields - these links are internal to the document.

    'Reset links to InlineShapes
    For Each InShp In ActiveDocument.InlineShapes
        If Not InShp.LinkFormat Is Nothing Then
            InShp.LinkFormat.SourceFullName = WbkFullname
        End If
        If InShp.Hyperlink.Address <> "" Then
            InShp.LinkFormat.SourceFullName = WbkFullname
        End If
    Next
    
    'Reset links to fields
    For Each Fld In ActiveDocument.Fields
        If Not Fld.LinkFormat Is Nothing Then
            If Fld.LinkFormat.Type <> wdLinkTypeReference Then 
                Fld.LinkFormat.SourceFullName = WbkFullname
            End If
        End If
    Next
    
    0 讨论(0)
  • 2021-01-11 18:45

    Maybe not all Fields/Shapes are linked and the original insert of the field/shape resulted in not all properties being created on the object.

    To advance your code and find out in more detail what is the matter with the objects, try to ignore and report errors. Use watches to inspect the objects.

    For example:

    On Error Goto fieldError
    For Each aField In oDoc.Fields
        With aField
            .LinkFormat.AutoUpdate = False
            DoEvents
            .LinkFormat.SourceFullName = NewLink
            .Code.Text = Replace(.Code.Text, Replace(.LinkFormat.SourceFullName, "\", "\\"), Replace(NewLink, "\", "\\"))
            Goto fieldContinue
          fieldError:
            MsgBox "error: <your info to report / breakpoint on this line>"
          fieldContinue:
        End With
    Next aField
    

    P.s.: what is the purpose of DoEvents? That will process external events (Windows messages).

    0 讨论(0)
提交回复
热议问题