Excel VBA:如何使用Excel中的ListTemplates来控制Word?

Excel VBA:如何使用Excel中的ListTemplates来控制Word?,vba,excel,Vba,Excel,上述代码在Word VBA中运行良好,但在Excel中不起作用。 不知道为什么在Excel中使用ListGalleries来控制Word如此困难。。。 在网上找到了数百万条条目,但几乎找不到。 谁能帮点忙吗?我绝望了。。。 Word VBA的在线报道几乎为零 在Excel中,您需要添加对Word对象模型的引用: 在makro编辑器(Alt+F11)中,选择“工具”菜单并单击“参考…”。单击“Microsoft Word对象库”旁边的复选框。单击“确定”。现在再次尝试运行宏 这样你就快到了 我遇到

上述代码在Word VBA中运行良好,但在Excel中不起作用。 不知道为什么在Excel中使用ListGalleries来控制Word如此困难。。。 在网上找到了数百万条条目,但几乎找不到。 谁能帮点忙吗?我绝望了。。。
Word VBA的在线报道几乎为零

在Excel中,您需要添加对Word对象模型的引用:

在makro编辑器(Alt+F11)中,选择“工具”菜单并单击“参考…”。单击“Microsoft Word对象库”旁边的复选框。单击“确定”。现在再次尝试运行宏

这样你就快到了

我遇到了一些可能是兼容性问题的错误。您使用的是哪个版本的office?我正在Office 2010上测试所有这些

为了让它工作,我必须改变什么(至少我这么认为,我不知道最后一个循环到底要实现什么):

^交换范围设置参数,以便正确检测整个表(不确定这是否是您想要的,因为每次循环运行时都会调用它)


^参数ListTemplate应为ListTemplate对象。将temp3设置为ListTemplate中包含的ListLevel对象。同样,不确定这是否是您想要实现的目标,但根据Office 2010文档,这是应该实现的。

谢谢您的回复。以下是示例:
Sub test()

Dim wrdApp As Word.Application
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Dim wrdDoc As Word.Document
Set wrdDoc = wrdApp.Documents.Add

Dim wrdTbl As Word.Table
Set wrdTbl = wrdDoc.Tables.Add(Range:=wrdDoc.Range, NumRows:=6, NumColumns:=1)

With wrdTbl

.Borders(wdBorderTop).LineStyle = wdLineStyleSingle
.Borders(wdBorderLeft).LineStyle = wdLineStyleSingle
.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle
.Borders(wdBorderRight).LineStyle = wdLineStyleSingle
.Borders(wdBorderHorizontal).LineStyle = wdLineStyleSingle
.Borders(wdBorderVertical).LineStyle = wdLineStyleSingle

For r = 1 To 6
    .Cell(r, 1).Range.Text = ActiveSheet.Cells(r, 1).Value
Next r
End With

' Dim temp3 As ListGalleries
For r = 1 To 6 Step 2
Set temp3 = wrdApp.ListGalleries(wdNumberGallery).ListTemplates(1).ListLevels(1)
With temp3
    .NumberFormat = "%1."
    .TrailingCharacter = wdTrailingTab
    .NumberStyle = wdListNumberStyleArabic
    .NumberPosition = CentimetersToPoints(0.63)
    .Alignment = wdListLevelAlignLeft
    .TextPosition = CentimetersToPoints(1.27)
    .TabPosition = wdUndefined
    .StartAt = r
End With
Dim rng As Range
Set rng = wrdDoc.Range(Start:=wrdDoc.Range.Rows(1).Range.Start, End:=wrdDoc.Range.Rows(6).Range.End)
rng.ListFormat.ApplyListTemplate ListTemplate:=temp3
Next r

End Sub
Set rng = wrdDoc.Range(Start:=wrdTbl.Rows(1).Range.Start, End:=wrdTbl.Rows(6).Range.End)
rng.ListFormat.ApplyListTemplate ListTemplate:=wrdApp.ListGalleries(wdNumberGallery).ListTemplates(1)