我想从Excel VBA脚本生成MS Word文档。在这个例子中,我生成10行文本。在特定情况下(这里用随机变量表示),我想应用特殊列表格式。
在这种情况下,我收到错误:对象'ListFormat'的方法ApplyListTemplateWithLevel失败 我尝试了几种方法,但收效甚微
Sub ToWord()
' In Tools|References, add reference to "Microsoft Word 12 Object Library"
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim x As Integer
Set wdApp = New Word.Application
wdApp.Visible = True
wdApp.Activate
Set wdDoc = wdApp.Documents.Add
With wdApp.Selection
For x = 1 To 10
.TypeText ("a simple text string ")
.TypeText ("a simple text string ")
.TypeText ("a simple text string ")
.TypeText ("a simple text string ")
.TypeParagraph
If Rnd > 0.5 Then
' crashes here...
wdDoc.Range(.Paragraphs(x).Range.Start, .Paragraphs(x).Range.End) _
.ListFormat.ApplyListTemplateWithLevel ListTemplate:= _
ListGalleries(wdNumberGallery).ListTemplates(1), ContinuePreviousList:= _
False, ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
wdWord10ListBehavior
End If
Next x
End With
End Sub
答案 0 :(得分:0)
尝试更改
wdDoc.Range(.Paragraphs(x).Range.Start,.Paragraphs(x).Range.End)
到
wdDoc.Paragraphs(x)的.Range