我正在使用 Excel 2010 VBA 以编程方式生成 Word 2010 文档。
当我尝试将 ListTemplate 应用于我插入的段落之一(下面代码中的第 5 行)时,程序崩溃。
thisValue = tempSheet.Cells(i, 1).Value
.Content.InsertAfter thisValue
.Content.InsertParagraphAfter
Set thisRange = .Paragraphs(i).Range
thisRange.ListFormat.ApplyListTemplate ListTemplate:=ListGalleries(wdBulletGallery).ListTemplates(1)
抛出的错误如下所示:
运行时错误“-2147023170 (800706be)”
自动化错误
远程过程调用失败。
整个过程:
Sub MoveDataToWord(ByRef tempSheet As Worksheet)
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Add
With wrdDoc
For i = 1 To tempSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
thisValue = tempSheet.Cells(i, 1).Value
.Content.InsertAfter thisValue
.Content.InsertParagraphAfter
Set thisRange = .Paragraphs(i).Range
thisRange.ListFormat.ApplyListTemplate ListTemplate:=ListGalleries(wdBulletGallery).ListTemplates(1)
Next i
End With
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub