2011-12-13 57 views
0
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 近零在线报道...Excel VBA:如何在Excel中使用ListTemplates来控制Word?

回答

1

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

在万客隆编辑器(ALT + F11)选择“工具”菜单并点击“参考...”。单击“Microsoft Word对象库”旁边的复选框。点击“确定”。现在尝试再次运行宏。

这应该会让你快到那里。

我遇到了一些可能是兼容性问题的错误。你在哪个版本的办公室?我测试这一切在Office 2010中

我有什么改变,使其工作(至少我是这样认为的,不知道你想与过去的循环来实现什么):

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) 

^参数ListTemplate预计为ListTemplate对象。您将temp3设置为ListTemplate中包含的ListLevel对象。再次,不确定这是否是您要完成的内容,但根据Office 2010文档,这应该是。

+0

感谢您的回复。以下是插图: http://www.youtube.com/watch?v=tVZhav-E92w&feature=youtu.be –