简体   繁体   English

如何在带有 Excel VBA 的 Word 文档中使用 BuildingBlockEntry().insert 方法?

[英]How to use BuildingBlockEntry().insert method in Word Document with Excel VBA?

Most of the code is copied from How to use VBA to insert Excel data into Word, and export it as PDF?大部分代码复制自How to use VBA to insert Excel data into Word, and export it as PDF? . .

Is there is any way to insert text from quickparts-buildingblocks in a Word document via Excel VBA?有什么方法可以通过 Excel VBA 在 Word 文档中插入 Quickparts-buildingblocks 中的文本?

This freezes Excel:这会冻结 Excel:

wordDoc.Application.Templates(...).BuildingBlockEntries("test").Insert Where:=Selection.Range, RichText:=True

Code:代码:

Sub Generate()
Dim wb As Workbook
Set wb = ActiveWorkbook

Dim wsGenerator As Worksheet
Set wsGenerator = wb.Sheets("List")

Dim wordApp As Word.Application
Set wordApp = New Word.Application

Dim wordDoc As Word.Document
Dim name1, name2, name3, name4 As String
Dim n, j As Integer

n = wsGenerator.Range("A:A").Find(what:="*", searchdirection:=xlPrevious).Row

For j = 2 To n

    Set wordDoc = wordApp.Documents.Open("C:\Users\" & Environ("username") & "\Desktop\ExcelTest\Template.docx")
        
    wordApp.Templates.LoadBuildingBlocks

    name1 = wsGenerator.Range("A" & j).Value
    name2 = wsGenerator.Range("B" & j).Value
    name3 = wsGenerator.Range("C" & j).Value
    name4 = wsGenerator.Range("D" & j).Value

    If name4 = "" Then
        wordDoc.Application.Templates( _
          Environ("AppData") & "\Microsoft\Document Building Blocks\1045\16\Building Blocks.dotx" _
          ).BuildingBlockEntries("test").Insert Where:=Selection.Range, RichText:=True
    End If

    With wordDoc.Content.Find
        .Execute FindText:="<<name1>>", ReplaceWith:=name1, Replace:=wdReplaceAll
        .Execute FindText:="<<name2>>", ReplaceWith:=name2, Replace:=wdReplaceAll
        .Execute FindText:="<<name3>>", ReplaceWith:=name3, Replace:=wdReplaceAll
        .Execute FindText:="<<name4>>", ReplaceWith:=name4, Replace:=wdReplaceAll
    End With

    wordDoc.ExportAsFixedFormat "C:\Users\" & Environ("Username") & "\Desktop\ExcelTest\" & wsGenerator.Range("A" & j).Value & " " & wsGenerator.Range("C" & j).Value & ".pdf", _
      wdExportFormatPDF

    wordDoc.Close (wdDoNotSaveChanges)

Next
End Sub

There are several issues with your code.您的代码有几个问题。

Firstly, wordDoc.Application is going to fail because Application is not a child object of the document.首先, wordDoc.Application将失败,因为Application不是文档的子对象。 You have already set a variable, wordApp to point to the Word Application object and need to use that.您已经设置了一个变量wordApp指向 Word Application 对象并需要使用它。

Secondly, you only need to load the building blocks once, not during each iteration of the loop.其次,您只需要加载构建块一次,而不是在循环的每次迭代期间。

Thirdly, in VBA a variable declaration of: Dim name1, name2, name3, name4 As String will result in only name4 being a string whilst all the have the default datatype of Variant.第三,在 VBA 中,变量声明为: Dim name1, name2, name3, name4 As String将导致只有name4是一个字符串,而所有这些都具有 Variant 的默认数据类型。

With these issues corrected your code will be:纠正这些问题后,您的代码将是:

Dim wb As Workbook
Set wb = ActiveWorkbook

Dim wsGenerator As Worksheet
Set wsGenerator = wb.Sheets("List")

Dim wordApp As Word.Application
Set wordApp = New Word.Application

Dim wordDoc As Word.Document
Dim name1 As String, name2 As String, name3 As String, name4 As String
Dim n, j As Integer

n = wsGenerator.Range("A:A").Find(what:="*", searchdirection:=xlPrevious).Row

'load building blocks
Dim bblockSource As String
bblockSource = Environ("appdata") & "\Microsoft\Document Building Blocks\1045\16\Building Blocks.dotx"
wordApp.Templates.LoadBuildingBlocks

For j = 2 To n

    Set wordDoc = wordApp.Documents.Open("C:\Users\" & Environ("username") & "\Desktop\ExcelTest\Template.docx")
    

    name1 = wsGenerator.Range("A" & j).Value
    name2 = wsGenerator.Range("B" & j).Value
    name3 = wsGenerator.Range("C" & j).Value
    name4 = wsGenerator.Range("D" & j).Value

    If name4 = "" Then
        wordApp.Templates(bblockSource).BuildingBlockEntries("test").Insert Where:=wordApp.Selection.Range, RichText:=True
    End If

    With wordDoc.Content.Find
        .Execute FindText:="<<name1>>", ReplaceWith:=name1, Replace:=wdReplaceAll
        .Execute FindText:="<<name2>>", ReplaceWith:=name2, Replace:=wdReplaceAll
        .Execute FindText:="<<name3>>", ReplaceWith:=name3, Replace:=wdReplaceAll
        .Execute FindText:="<<name4>>", ReplaceWith:=name4, Replace:=wdReplaceAll
    End With

    wordDoc.ExportAsFixedFormat "C:\Users\" & Environ("Username") & "\Desktop\ExcelTest\" & wsGenerator.Range("A" & j).Value & " " & wsGenerator.Range("C" & j).Value & ".pdf", _
        wdExportFormatPDF

    wordDoc.Close (wdDoNotSaveChanges)

Next

You also need to be aware that your code does not shut Word down after you have finished with it which could lead to multiple hidden instances of Word.您还需要注意,您的代码在完成后不会关闭 Word,这可能会导致 Word 的多个隐藏实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM