问题描述
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
以上代码在Word VBA中效果很好,但在Excel中效果不佳.不知道为什么在Excel中使用ListGalleries来控制Word如此困难...在网上找到了数百万条条目,但几乎找不到.有人可以帮忙吗?我很绝望...几乎没有Word VBA的在线报道...
The above codes work well in Word VBA but not in Excel.Don't know why so difficult to use ListGalleries in Excel to control Word...Have found millions of entries online but could hardly find one.Could anyone please help a bit? I'm desperate...Near nil online coverage on Word VBA...
推荐答案
在Excel中,您需要添加对Word对象模型的引用:
In Excel you need to add a reference to the Word object model:
在makro编辑器(Alt + F11)中,选择工具"菜单,然后单击参考...".单击"Microsoft Word对象库"旁边的复选框.点击确定".现在,尝试再次运行宏.
In the makro editor (Alt+F11) select the "Tools" menu and click "References...". Click the checkbox next to "Microsoft Word Object Library." Click "OK." Now try to run the macro again.
那应该可以带你到那儿了.
That should get you nearly there.
我遇到了一些可能是兼容性问题的错误.您使用的是哪个版本的Office?我正在Office 2010上测试所有这些.
I encountered a few errors that could be compatibility issues. Which version of office are you on? I'm testing all this on Office 2010.
要使其正常运行,我必须进行更改(至少我认为是这样,不知道您在最后一个循环中到底要实现什么目标):
What I had to change to make it work (at least I think so, don't know what exactly you're trying to achieve with that last loop):
Set rng = wrdDoc.Range(Start:=wrdTbl.Rows(1).Range.Start, End:=wrdTbl.Rows(6).Range.End)
^交换了范围设置参数,以便正确检测整个表(不确定这是否是您想要的,因为每次循环运行时都会调用该表).
^ Exchanged the range-setting parameters so the whole table gets detected correctly (not sure if this is what you want because this gets called every time the loop is running).
rng.ListFormat.ApplyListTemplate ListTemplate:=wrdApp.ListGalleries(wdNumberGallery).ListTemplates(1)
^参数ListTemplate应该作为ListTemplate对象.您将temp3设置为ListTemplate中包含的ListLevel对象.同样,不确定这是否是您要完成的工作,但是根据Office 2010文档,这应该是应该的.
^ The parameter ListTemplate is expected as a ListTemplate object. You set temp3 to the ListLevel-object that is contained within the ListTemplate. Again, not sure if this is what you're trying to accomplish but according to Office 2010 documentation this is how it should be.
这篇关于Excel VBA:如何在Excel中使用ListTemplates来控制Word?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!