我使用Excel2010VBA以编程方式生成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
发布于 2012-07-13 15:50:25
ListGalleries是Word Application中的一个对象。要访问它,我需要使用wrdApp.ListGalleries。固定代码行如下所示。
thisRange.ListFormat.ApplyListTemplate ListTemplate:=wrdApp.ListGalleries(wdBulletGallery).ListTemplates(1)
https://stackoverflow.com/questions/11472074
复制相似问题