VBA for Word-UNC路径

问题描述

我有一个Word文档,该文档使用VBA表单根据选择框生成文档内容。 VBA将其他Word文档复制并粘贴到主文档中。我使用显式路径指定应在何处找到单词document。

最好使用UNC路径,以删除文件位置的某些“显式性”(c:\ Files \ Example \ Content \ PROFILE.docx),并用“ .. \”代替Content \ Profile.docx“ 代码示例:

Sub GenerateProfile()

  Dim currentPathProfile As String

  currentPathProfileText = ActiveDocument.Path

  currentPathProfileText = currentPathProfileText & "c:\Files\Example\Content\PROFILE.docx"

  Documents.Open FileName:=currentPathProfileText
  
  Dim currentPathProfileDoc As Document

  Set currentPathProfileDoc = Documents(1)

  currentPathProfileDoc.Activate
  
  Call copyWholeContent

  currentPathProfileDoc.Close

  Call PasteWholeContent

End Sub

解决方法

如果其他Word文档在保存代码的模板的相对路径位置,则可以执行此操作。如果它们位于子文件夹中,将更加容易。生成ThisDocument(您的代码持有人)的路径,并将其用作基础。 ThisDocument.Path

但是,为什么不从文档中提取文本,为什么不以自动图文集的形式将文本存储在模板中呢?如果需要多个模板中的信息,则可以将其作为自动图文集存储在全局模板中。 IMO,这更加灵活,不易出现问题。