【问题标题】:Issue using Excel VBA to add Watermark to Word Document using BuildingBlockEntry使用 Excel VBA 使用 BuildingBlockEntry 向 Word 文档添加水印的问题
【发布时间】:2017-03-10 22:53:34
【问题描述】:

我目前正在尝试使用 Excel VBA 向 Word 文档添加水印。我已经能够从 Word VBA 执行此操作,并将代码转换为 excel 以实现其他功能,并且在特定行上出现错误。我相信在请求插入水印时我需要更好地指向 Word 构建块,但我不确定。

错误是来自以下行的“请求的集合成员不存在”:oWord.Templates(strBBPath).BuildingBlockEntries(strBBName).Insert Where:=oRng, RichText:=True

这是我的代码:

    Sub AddWatermark()
     Dim oWord as Word.Application
     Dim oDoc As Word.Document
     Dim oSection As Word.section
     Dim oHeader As Word.HeaderFooter
     Dim oRng As Word.Range
     Dim strName As String
     Dim strPath As String
     Dim strBBPath As String
     Const strBBName As String = "SAMPLE 1" 'The building block name that you want to insert

     strBBPath = "C:\Users\" & (Environ$("Username")) & "\AppData\Roaming\Microsoft\Document Building Blocks\1033\14\Built-In Building Blocks.dotx"

               Dim lngCount As Long

               ' Open the file dialog
               With Application.FileDialog(msoFileDialogOpen)
                   .AllowMultiSelect = True
                   .Show

                   ' Display paths of each file selected
                   For lngCount = 1 To .SelectedItems.Count
                        Set oWord = New Word.Application
                        strPath = .SelectedItems(lngCount)
                        Set oDoc = oWord.Documents.Open(strPath)
                   Next lngCount
               End With

     'oDoc.Save 'save the document
     strName = oDoc.FullName 'Record the document name
     oWord.Visible = True

     'Address each section
     For Each oSection In oDoc.Sections
         'Address each header in the section
         For Each oHeader In oSection.Headers

             Set oRng = oHeader.Range
             oRng.Start = oRng.End 'set the range to the end of the header
             'Insert the built-in building block
             oWord.Templates(strBBPath).BuildingBlockEntries(strBBName).Insert Where:=oRng, RichText:=True

         Next oHeader
     Next oSection

     End Sub

【问题讨论】:

    标签: vba excel ms-word watermark buildingblocks


    【解决方案1】:

    除非您在代码的其他地方有一个杂散的Word 标识符,否则不知道为什么会收到该特定错误消息。如果您在 Excel 中运行它,它应该是“运行时错误 424 - 需要对象”。您无权访问 Excel 中的全局 Word 对象,因此在这一行中...

    Word.Application.Templates(strBBPath).BuildingBlockEntries(strBBName).Insert Where:=oRng, RichText:=True
    

    ...Excel 不知道Word 是什么。 Option Explicit 在你的模块顶部会发现这个错误。您需要使用您的 Word.Application 对象:

    oWord.Templates(strBBPath).BuildingBlockEntries(strBBName).Insert Where:=oRng, RichText:=True
    

    也就是说,您显然在使用早期绑定,所以将 oWord 声明为 Word.Application...

    Dim oWord As Word.Application
    

    ...并使用New 而不是CreateObject

    Set oWord = New Word.Application
    

    【讨论】:

    • 感谢您澄清所有这些,我已经进行了这些更新。我仍然收到“运行时错误 5941 - 请求的集合成员不存在”的错误,我想知道是否需要以不同于 excel 的方式引用模板或构建块条目,但不确定。
    【解决方案2】:

    我从这篇文章中找到了答案 - https://answers.microsoft.com/en-us/msoffice/forum/msoffice_word-mso_other/run-time-error-5941-not-always-but-annoyingly/b95ec1db-6928-4d87-b799-52d4f1c01f08

    使用此方法需要加载 Word 构建块以从中提取:oWord.Templates.LoadBuildingBlocks

    感谢所有观看的人。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-27
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多