Reputation: 1296
I'm using the following macro to iterate through Word Documents and remove color from all of the tables in them except for the first table.
We have documents that absolutely will not work with Form Fields due to form protection issues with an application that auto-fills these documents with merge fields. So, the next best thing was to just mark what still needs to be filled in with shading and then remove the shading before printing the document. When this code iterates through the document though, it removes some of the borders for certain tables.
I definitely don't want this, I just want to remove the shading. I have no idea why it's doing this, so if anyone has any insight on that, that would be most helpful. If not, if I could adjust this macro to only change cells that have non-white background color, that would work too.
I've tried several variants of a nested loop, but could not get it to run that way.
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
I have also tried this code and got the same effect of borders being removed:
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
EDIT: While I still can't see what specifically is making these tables lose their borders, I've found that splitting the tables up in a certain way causes them to NOT lose their borders. I've tried my best to isolate this with no luck, as it seems that only a certain combination of things causes the loss of borders. However, at least I've got something that works. If anyone can provide a macro that would iterate through the individual cells as initially requested, it probably wouldn't be a bad option to have in the back pocket.
Upvotes: 1
Views: 4446
Reputation: 1296
Finally figured out a Macro to iterate through cells. For some reason, I couldn't get that nested for each loop to work until I tried this one. All of the shaded cells are the same shade of gray, so I just compared each cell and changed it only if it was gray. Not the most efficient way to do it, but since these documents are fairly small, this works fine.
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
Upvotes: 0
Reputation: 149335
You have to use .Shading
to remove the background.
I will demostrate it for one document. Please adapt it as per your code.
Let's say your document looks like this
Try this code
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
This is how your document looks after the code runs
Upvotes: 1