使用VBA使用当前文档的路径更新LINK字段的最佳方法是什么?

时间:2018-02-09 00:24:09

标签: vba ms-word word-vba

所以我有4个文件,3个excel电子表格和1个文件。所有四个都在同一个目录“test”。无论如何,所有四个将始终保持在同一目录中。但是,该文档的目标是从多个属性的三个电子表格中构建报告。这意味着它们使用的每台计算机的路径都不同。我想要一个宏,它将使用当前路径自动更新LINK字段,但我遇到了一些麻烦。

到目前为止我已经

SendKeys "%{F9}"
Dim path As String
path = ActiveDocument.path
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "C:\\Users\\Gianni\\Desktop"
.Replacement.Text = path
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
SendKeys "%{F9}"

从我所知道的,这有两个问题。如果我只是手动查看字段并在没有第一个SendKeys命令的情况下运行代码,那么find&替换工程。但是,使用第一个SendKeys命令,代码不会用新路径替换文本。不过,无论如何,粘贴的路径最终会打破链接。我该如何解决这些问题?

3 个答案:

答案 0 :(得分:1)

您可以使用以下代码显示字段代码,而不是使用SendKeys

ActiveDocument.ActiveWindow.View.ShowFieldCodes = True

并显示字段值

ActiveDocument.ActiveWindow.View.ShowFieldCodes = False

可以帮助解决您的第一个问题。

答案 1 :(得分:1)

通常,在Word中使用Word文档的基础对象模型比在尝试准确再现用户所做的更好。从用户的角度理解Word的工作方式非常重要,通过将这些步骤转换为宏,您可以做很多事情。但是挖掘对象模型通常更快,更准确。

更改LINK字段代码就是其中之一 - 就像许多事情一样,有不止一种方法可以实现。这有两种可能性。

通过操作字段代码,第一个接近你接近它的方式。请注意,使用VBA实际上显示字段代码并不是必需的。对象模型允许您“在幕后”操作它。

此过程循环文档中的所有字段,检查每个字段是否为LINK字段。如果是,则使用VBA Replace函数在原始路径的字段代码中替换备用路径,然后将其写入字段代码。

'Assumes the linked Excel workbook is an inline shape
Sub ChangePathInLinkField()
    Dim doc As word.Document
    Dim fld As word.Field
    Dim strSearchPath As String
    Dim strReplacePath As String
    Dim strNewFieldCode As String

    Set doc = ActiveDocument
    strSearchPath = "C:\\Users\\[user name]\\Documents\\SampleChart.xlsx"
    strReplacePath = "C:\\Test\\SampleChart.xlsx"
    For Each fld In doc.Fields
      If fld.Type = wdFieldLink Then
        strNewFieldCode = Replace(fld.code.Text, strSearchPath, strReplacePath)
        fld.code.Text = strNewFieldCode
      End If
    Next
    doc.Fields.Update
End Sub

第二个过程显示如何为Shapes和InlineShapes更改链接路径(如果您有一个Shape,则无法看到LINK字段)。当然,它也可以仅用于InlineShapes。这将循环集合,检查对象是否是链接的OLE对象,如果是,则更改路径。

使用哪一个取决于你的情况 - 测试它们并根据它来决定。

'Alternate: works with OLE object
Sub ChangePathInLinkedObject()
    Dim doc As word.Document
    Dim ils As word.InlineShape
    Dim shp As word.Shape
    Dim strReplacePath As String
    Dim i As Long

    Set doc = ActiveDocument
    strReplacePath = "C:\Users\Cindy Meister\Documents\SampleChart.xlsx"
    strReplacePath = "C:\Test\SampleChart.xlsx"
    'For Each doesn't work because updating the field
    'destroys the object, so it loops over the same object
    'For this reason it's also necessary to work backwards through the document
    For i = doc.InlineShapes.Count To 1 Step -1
        Set ils = doc.InlineShapes(i)
        If ils.Type = wdInlineShapeLinkedOLEObject Then
            ils.LinkFormat.SourceFullName = strReplacePath
            ils.LinkFormat.Update
        End If
    Next

    For i = doc.shapes.Count To 1 Step -1
        Set shp = doc.shapes(i)
        If shp.Type = msoLinkedOLEObject Then
            shp.LinkFormat.SourceFullName = strReplacePath
            shp.LinkFormat.Update
        End If
    Next
End Sub

答案 2 :(得分:-1)

要了解如何在Word中实现相对路径,请查看我发布的解决方案: http://windowssecrets.com/forums/showthread.php/154379-Word-Fields-and-Relative-Paths-to-External-Files 由于您正在使用LINK字段,因此您需要使用宏解决方案。