PDA

View Full Version : Auto changing table shading (Help)



Logicpro8_us
07-05-2010, 02:35 AM
Hi Guys!

Im using word 2003 and have about 200 word files that I need to change the shading of the tables in.

Just now they are Gary 20% and I need them to be Gray 15% with style clear.

I have never touched on VB beofre but really want to get to know it a bit better.

Any pointers or help would be great! :banghead:

gmaxey
07-05-2010, 05:03 AM
Try something like this:

Sub ScratchMaco()
Dim oTbl As Word.Table
Dim oCell As Cell
Dim oDoc As Word.Document
Set oDoc = ActiveDocument
For Each oTbl In oDoc.Tables
For Each oCell In oTbl.Range.Cells
If oCell.Shading.BackgroundPatternColor = wdColorGray20 Then
oCell.Shading.BackgroundPatternColor = wdColorGray15
End If
Next oCell
Next oTbl
Set oDoc = Nothing
End Sub

gmaxey
07-05-2010, 05:07 AM
You would probably want to process all the documents from a batch folder. You could make the code I provided earlier the "user defined" code and using the following AddIn:

http://gregmaxey.mvps.org/Process_Batch_Folder.htm

Logicpro8_us
07-05-2010, 05:32 AM
Thanks for getting back to me guys!

I tried



Sub ScratchMaco()
Dim oTbl As Word.Table
Dim oCell As Cell
Dim oDoc As Word.Document
Set oDoc = ActiveDocument
For Each oTbl In oDoc.Tables
For Each oCell In oTbl.Range.Cells
If oCell.Shading.BackgroundPatternColor = wdColorGray20 Then
oCell.Shading.BackgroundPatternColor = wdColorGray15
End If
Next oCell
Next oTbl
Set oDoc = Nothing
End Sub


But it didnt do anyting, I suspect that this is caused by the line:


If oCell.Shading.BackgroundPatternColor = wdColorGray20 Then


You see the problem is I dont think the tables have Gray20%.

I have uplioaded a test word file for you to see.
The files will be added to one big word file before I do this, so need for batch just now.

4007

Logicpro8_us
07-05-2010, 06:12 AM
I done it fine with a simple record, but its for selected cells only. Anyway of making it just for the gray cells in the document?


Sub Macro1()
'
' Macro1 Macro
' Macro recorded 05/07/2010 by Image v7.10P
'
With Selection.Cells
With .Shading
.Texture = wdTextureNone
.ForegroundPatternColor = wdColorAutomatic
.BackgroundPatternColor = wdColorGray15
End With
With .Borders(wdBorderLeft)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderRight)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderTop)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
With .Borders(wdBorderBottom)
.LineStyle = wdLineStyleSingle
.LineWidth = wdLineWidth050pt
.Color = wdColorAutomatic
End With
.Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
.Borders(wdBorderDiagonalUp).LineStyle = wdLineStyleNone
.Borders.Shadow = False
End With
With Options
.DefaultBorderLineStyle = wdLineStyleSingle
.DefaultBorderLineWidth = wdLineWidth050pt
.DefaultBorderColor = wdColorAutomatic
End With
End Sub

gmaxey
07-05-2010, 08:09 AM
This is crude but I don't have time to refine it. It appears that someone has applied paragraph formatting to the table. Try:

Sub ScratchMaco()
Dim oPar As Paragraph
Dim i As Long
For Each oPar In ActiveDocument.Range.Paragraphs
If oPar.Shading.BackgroundPatternColorIndex = 65535 Then
oPar.Shading.BackgroundPatternColorIndex = wdAuto
oPar.Range.Rows(1).Select
For i = 2 To 4
Selection.Cells(i).Shading.BackgroundPatternColor = wdColorGray15
Next i
End If
Next
End Sub

Logicpro8_us
07-05-2010, 08:21 AM
This works great, only one thing....

It errors when it hits a "vertically merged cell"


I wonder how to refine it more to over come this, it speeds through the document and then Boom. It hit lots of "vertically merged cell"

fumei
07-07-2010, 08:16 AM
"It errors when it hits a "vertically merged cell"

There is NO solution to this. VBA does not function (and for very good reasons) on tables with vertically merged cells. It is for this reason that merged cells are to be avoided as much as possible. I never, ever, ever, use them.

Bottom line? If you have merged cells you can not action them with VBA.