2011-12-13 60 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 –