2013-02-19 89 views
1

我正在使用以下宏遍歷Word文檔,並從第一個表格除外的所有表格中移除顏色。用於從特定表格單元格中移除顏色的字宏

由於與自動填充這些文檔與合併域的應用程序的表單保護問題,我們有絕對不會使用表單域的文檔。因此,下一個最好的做法是隻標記需要用陰影填充的內容,然後在打印文檔之前刪除陰影。當這段代碼遍歷文檔時,它刪除了某些表的一些邊界。

我絕對不想要這個,我只是想刪除陰影。我不知道爲什麼它這樣做,所以如果任何人有任何的洞察力,那將是最有幫助的。如果沒有,如果我可以調整這個宏只改變具有非白色背景色的單元格,那也可以。

我試過幾個嵌套循環變體,但不能讓它以這種方式運行。

Sub decolordocument() 
    Dim tbl As Table 
    Dim first As Boolean 

    first = True 

    For Each tbl In ActiveDocument.Tables 
     If first Then 
      first = False 
     Else 
      tbl.Shading.BackgroundPatternColor = wdColorWhite 
     End If 
    Next 

    MsgBox "Shaded cells in tables have been updated." 
End Sub 

我自己也嘗試這個代碼,並得到了被刪除的邊界同樣的效果:

Sub decolordocument() 

    Dim tbl As Table 
    Dim tblCount As Long 
    Dim i As Long 
    Dim first As Boolean 

    tblCount = ActiveDocument.Tables.Count 

    For i = 2 To tblCount 
     With ActiveDocument.Tables(i).Shading 
      .BackgroundPatternColor = wdColorWhite 
     End With 
    Next 
    MsgBox "Shaded cells in tables have been updated." 

End Sub 

編輯:雖然我還看不到具體是什麼正在這些表失去他們的邊界,我我們發現以某種方式分割表格會使他們不會失去邊界。我已經盡了最大努力來隔離這個問題,因爲似乎只有某些特定的組合會導致邊界的損失。不過,至少我有一些工作。如果任何人都可以提供一個可以按照初始請求遍歷各個單元格的宏,那麼在後面的口袋裏可能不會是一個不好的選擇。

+0

我覺得你忘了在'下一步'之前的'結束如果'。請更正您的代碼 – Saju 2013-02-19 16:23:28

+0

不,結束語句在那裏。 – 2013-02-19 17:01:14

+0

你的錯誤已被@Siddharth Rout糾正,請點擊「'編輯」後的鏈接查看更改:) – Saju 2013-02-19 17:07:11

回答

0

後最後的樣子figu紅出一個宏來遍歷單元格。出於某種原因,我無法得到每個循環的嵌套工作,直到我嘗試這個。所有陰影單元格都是相同的灰色陰影,所以我只比較了每個單元格,並且只在灰色時才更改它。不是最有效的方式,但由於這些文件相當小,這工作正常。

Sub decolordocument() 

Dim tbl As Table 
Dim tblCount As Long 
Dim cll As Word.Cell 
Dim i As Long 

tblCount = ActiveDocument.Tables.Count 

For i = 2 To tblCount 
    For Each cll In ActiveDocument.Tables(i).Range.Cells 
     If cll.Shading.BackgroundPatternColor = RGB(217, 217, 217) Then 
      cll.Shading.BackgroundPatternColor = wdColorWhite 
     End If 
    Next 
Next 
MsgBox "Color in shaded cells has been removed." 

End Sub 
1

您必須使用.Shading刪除背景。

我將爲它展示一個文檔。請根據您的代碼進行調整。

比方說,你的文件看起來像這樣

enter image description here

試試這個代碼

Option Explicit 

Sub Sample() 
    Dim tblCount As Long 
    Dim i As Long 

    '~~> Get the Tables Count 
    tblCount = ActiveDocument.Tables.Count 

    '~~> If number of tables is less than 2 then exit sub 
    If tblCount < 2 Then Exit Sub 

    '~~> Start with 2nd table and loop 
    For i = 2 To tblCount 
     '~~> Remove background 
     With ActiveDocument.Tables(i).Shading 
      .Texture = wdTextureNone 
      .ForegroundPatternColor = wdColorAutomatic 
      .BackgroundPatternColor = wdColorAutomatic 
     End With 
    Next 
End Sub 

這是您的文檔的代碼運行

enter image description here

+0

一個很好的建議,但這仍然會刪除我的一些表格邊框。再次,不知道爲什麼會發生。 – 2013-02-19 17:03:22

+0

你能分享你正在使用的代碼嗎?還要檢查這些受影響的表格是否有邊界。您可以通過手動刪除背景來完成此操作;) – 2013-02-19 17:06:38

+0

您是否可以在www.wikisend.com上傳受影響的文檔並在此共享鏈接? – 2013-02-19 18:31:57

相關問題