P
Peter Rooney
Hello, Word VBA folk!
Please excuse me in advance for what must seem a daft question, but this is
the first time I've posted in Word VBA - I mostly do Excel and Project!
The ultimate aim of my code is to produce a checkerboard effect on any
table, so that, for example, cells on row 1 are black red, black re and on
row 2 are red black red black, and so on.
If anyone would like to help me with that, then great!
However, I was trying to produce a macro that would set all cells in the
table back to white, if my main code goofed up. Here is what I wrote:
Sub AReset()
ThisTable = ActiveDocument.Tables(1)
ColumnCount = ThisTable.Columns.Count
RowCount = ThisTable.Rows.Count
MsgBox (RowCount & " rows by " & ColumnCount & " columns")
For RowCounter = 1 To RowCount
For ColumnCounter = 1 To ColumnCount
MsgBox ("Row " & RowCounter & ", Column " & ColumnCounter)
'CellToColour = ActiveDocument.Tables(1).Cell(Row:=RowCounter,
Column:=ColumnCounter)
CellToColour = ThisTable.Cell(Row:=RowCounter,
Column:=ColumnCounter)
CellToColour.Shading.BackgroundPatternColorIndex = wdWhite
Next ColumnCounter
Next RowCounter
End Sub
I declared a variable "ThisTable" to refer to "activedocument.tables(1)"
The lines:
ColumnCount = ThisTable.Columns.Count
RowCount = ThisTable.Rows.Count
work fine, but when I tried:
CellToColour = ThisTable.Cell(Row:=RowCounter,
Column:=ColumnCounter)
I got "Object doesn't support this property or method"
although, obviously, if I replaced the offending line with:
CellToColour = ActiveDocument.Tables(1).Cell(Row:=RowCounter,
Column:=ColumnCounter)
this worked OK. It's only a small point, but it seems a bit unwieldy to have
to keep referring to a table in this longhanded way. Can anyone help?
Cheers
Pete
Please excuse me in advance for what must seem a daft question, but this is
the first time I've posted in Word VBA - I mostly do Excel and Project!
The ultimate aim of my code is to produce a checkerboard effect on any
table, so that, for example, cells on row 1 are black red, black re and on
row 2 are red black red black, and so on.
If anyone would like to help me with that, then great!
However, I was trying to produce a macro that would set all cells in the
table back to white, if my main code goofed up. Here is what I wrote:
Sub AReset()
ThisTable = ActiveDocument.Tables(1)
ColumnCount = ThisTable.Columns.Count
RowCount = ThisTable.Rows.Count
MsgBox (RowCount & " rows by " & ColumnCount & " columns")
For RowCounter = 1 To RowCount
For ColumnCounter = 1 To ColumnCount
MsgBox ("Row " & RowCounter & ", Column " & ColumnCounter)
'CellToColour = ActiveDocument.Tables(1).Cell(Row:=RowCounter,
Column:=ColumnCounter)
CellToColour = ThisTable.Cell(Row:=RowCounter,
Column:=ColumnCounter)
CellToColour.Shading.BackgroundPatternColorIndex = wdWhite
Next ColumnCounter
Next RowCounter
End Sub
I declared a variable "ThisTable" to refer to "activedocument.tables(1)"
The lines:
ColumnCount = ThisTable.Columns.Count
RowCount = ThisTable.Rows.Count
work fine, but when I tried:
CellToColour = ThisTable.Cell(Row:=RowCounter,
Column:=ColumnCounter)
I got "Object doesn't support this property or method"
although, obviously, if I replaced the offending line with:
CellToColour = ActiveDocument.Tables(1).Cell(Row:=RowCounter,
Column:=ColumnCounter)
this worked OK. It's only a small point, but it seems a bit unwieldy to have
to keep referring to a table in this longhanded way. Can anyone help?
Cheers
Pete