Append formatted text to a table cell

A

Andrew Bernhardt

How do I append formatted text to a table cell that has text with a
different format (i.e. append a word in blue letters to a cell that has
words in other colors)? If I set a range to a cell and collapse it to the
end of the range, the text will get added to the next cell.

For example, if a document has one table and I want to append the word
"TEST" in red letters to cell(1,3), the following does not work:


Dim R As Range
Set R = ThisDocument.Tables(1).Cell(1, 3).Range
R.Collapse wdCollapseEnd
R.InsertAfter "TEST"
R.Font.Color = wdColorRed

The word is instead added to the beginning of cell(1,4).


- Andrew Bernhardt
 
J

Jean-Guy Marcil

Andrew Bernhardt was telling us:
Andrew Bernhardt nous racontait que :
How do I append formatted text to a table cell that has text with a
different format (i.e. append a word in blue letters to a cell that
has words in other colors)? If I set a range to a cell and collapse
it to the end of the range, the text will get added to the next cell.

For example, if a document has one table and I want to append the word
"TEST" in red letters to cell(1,3), the following does not work:


Dim R As Range
Set R = ThisDocument.Tables(1).Cell(1, 3).Range
R.Collapse wdCollapseEnd
R.InsertAfter "TEST"
R.Font.Color = wdColorRed

The word is instead added to the beginning of cell(1,4).

This is because the cell range includes the end of cell marker (¤). So, when
you collapse, the range starts *after* the cell marker in cell 1,3, i.e at
the beginning of cell 1,4.

Try this instead:

'_______________________________________
Dim R As Range

Set R = ActiveDocument.Tables(1).Cell(1, 3).Range
With R
.MoveEnd wdCharacter, -1
.Collapse wdCollapseEnd
.InsertAfter "TEST"
.Font.Color = wdColorRed
End With
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
[email protected]
Word MVP site: http://www.word.mvps.org
 
Top