Cell Object/Collection

G

Greg Maxey

If I create an table and add some cell specific shading then I can run this
code to change the shading:

Sub Test()
Dim oCell As Word.Cell
Dim oTbl As Word.Table
For Each oTbl In ActiveDocument.Tables
For Each oCell In oTbl.Range.Cells
If oCell.Shading.BackgroundPatternColor = RGB(229, 170, 91) Then
oCell.Shading.BackgroundPatternColor = RGB(93, 143, 86)
End If
Next
Next
End Sub

This works for a uniform table and it works on tables where cells are merge
horizontally. However, if I merge cells vertically then this code misses
the vertical merged cells.

If a vertically merged cell isn't a cell or part of a table cell collection
then what is it?

Thanks.
 
R

Russ

Greg,
If I comment out your "If" test, it shades every cell in my 'virgin' test
table with three middle cells merged vertically?
And msgbox activedocument.tables(1).range.cells.count returns an accurate
cell count after merging.
Are you merging cells after a shading is applied and that is skewing the
background shading "If" test? Like the font property undefined code 999...?

Sub Test()
Dim oCell As Word.Cell
Dim oTbl As Word.Table
For Each oTbl In ActiveDocument.Tables
For Each oCell In oTbl.Range.Cells
' If oCell.Shading.BackgroundPatternColor = RGB(229, 170, 91) Then
oCell.Shading.BackgroundPatternColor = RGB(93, 143, 86)
'End If
Next
Next
End Sub
 
R

Russ

Greg,
Experimenting further, I was able to duplicate the problem when rows
contained both vertical and horizontal merged cells. I put a message box
line and a oCell.Select line in the loop and I always saw the msgbox text
but some cells didn't become selected or shaded.

I tried to use range and that seemed to work.
For example:
oCell.Range.Shading.BackgroundPatternColor = RGB(93, 143, 86)

So it appears that the range object works better than the 'default'
selection object in this situation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top