0

I am currently trying to use Excel VBA to add a watermark to a Word document. I have been able to do so from Word VBA and have translated the code over to excel to implement other functions, and am getting an error on a specific line. I believe I need to better point to the Word building block when requesting the insert of the watermark, but am unsure.

The error is "The requested member of the collection does not exist" from the line: oWord.Templates(strBBPath).BuildingBlockEntries(strBBName).Insert Where:=oRng, RichText:=True

Here is my code:

    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
4

2 回答 2

1

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

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

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

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

也就是说,您显然正在使用早期绑定,因此声明oWordWord.Application...

Dim oWord As Word.Application

...并使用New而不是CreateObject

Set oWord = New Word.Application
于 2017-03-09T19:20:23.280 回答
0

我从这篇文章中找到了答案 - 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

感谢所有看过的人。:)

于 2017-03-10T14:53:34.277 回答