2013-01-08 47 views
2

我有我使用刪除所有在特定Word文檔表格的所有顏色的宏。被刪除的顏色最初表示某人應該輸入的位置。改變顏色,而是在Word中一個表VBA

相信我,我寧願使用表單域或ActiveX文本框,但這不是一種情況如Word正在通過與郵件合併無效這些第三方應用程序打開他們將工作。無論如何,我想跳過第一張桌子。我將下面的代碼設置爲執行此操作,然後將第一個表格的第一個單元格更改爲特定的顏色。

Sub decolordocument() 
' 
' decolordocument Macro 
' 
' 
Dim tbl As Table 

For Each tbl In ActiveDocument.Tables 
tbl.Shading.BackgroundPatternColor = wdColorWhite 
Next 
ActiveDocument.Tables(1).Cell(1, 1).Shading.BackgroundPatternColor = wdColorLightTurquoise 

End Sub 

這工作得很好去除顏色,但在第一個表格是第一個單元格的顏色是不是在所有的人都一樣。我只想在每個循環中跳過第一個表格。我試過一個if語句(如果TBL = ActiveDocument.Tables(1),然後...),但顯然這不是允許的比較,因爲它不承認當時的聲明。我也試着用範圍來做這個,但是不太對。任何想法將不勝感激。

回答

2
Sub decolordocument() 
' 
' decolordocument Macro 
' 
' 
Dim first as Boolean 
Dim tbl As Table 

first = true 

For Each tbl In ActiveDocument.Tables 
If first Then 
first = false 
Else 
tbl.Shading.BackgroundPatternColor = wdColorWhite 
End If 
Next 
'ActiveDocument.Tables(1).Cell(1, 1).Shading.BackgroundPatternColor = wdColorLightTurquoise 

End Sub 
+0

哇哦......說說咄!完美的作品。謝謝你的幫助。 –

0
if activedocument.Tables.Count >1 then 

     for x = 2 to activedocument.Tables.Count 

    activedocument.Tables(x).Shading.BackgroundPatternColor = wdColorWhite 

     next x 

    end if