Inserting text in a table cell

O

Ogier

The code snippet below works as intended: It inserts in the main document
some text in bold with different font sizes:

Dim rng As Word.Range
Set rng = Selection.Range
With rng
.Text = "Table "
.Font.Bold = True
.Collapse wdCollapseEnd
.InsertAfter " " & "Title"
.Font.Grow
.Collapse wdCollapseEnd
.InsertAfter Chr(11) & "Subtitle"
.Font.Shrink
.Font.Shrink
.Collapse wdCollapseEnd
.Font.Grow
.Font.Grow
.Font.Bold = False
End With

However, if I use it in a table,
Set rngCell = tbl.Rows(1).Range

I get "RTE 5251 This action is illegal at the end of a row"
The row consists of a single cell (created by merging other cells.

If I try to insert in a cell which is not the last one in its row, using
Set rngCell = tbl.Cell(4, 1).Range
the text 'Table' is written in this cell, but the rest of the text spills over
into cell (4,2).

I have also tried to do the merging of the cells in the first row *after*
inserting the text. But in this case an unwanted like break appears after
'Table'.

So how do I insert differntly formattet text into a single cell of a table?

Best regards
Holger Nielsen
 
C

Cindy M.

Hi Ogier,
However, if I use it in a table,
Set rngCell = tbl.Rows(1).Range

I get "RTE 5251 This action is illegal at the end of a row"
The row consists of a single cell (created by merging other cells.

If I try to insert in a cell which is not the last one in its row, using
Set rngCell = tbl.Cell(4, 1).Range
the text 'Table' is written in this cell, but the rest of the text spills over
into cell (4,2).

I have also tried to do the merging of the cells in the first row *after*
inserting the text. But in this case an unwanted like break appears after
'Table'.

So how do I insert differntly formattet text into a single cell of a table?

The trick is in understanding that Ranges in a Table include the table
structure, i.e. End-of-cell and end-of-row markers. So if you pick up a cell's
range:
Set rng cell = tbl.rows(1).Range

You should then collapse that range to its starting pont
rngCell.Collapse(wdCollapseStart)

Doing this puts the "insertion point" at the start of the cell (think blinking
cursor), leaving out the end-of-cell structural elements. From here, you start
assigning text, etc. as your larger code snippet shows.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
O

Ogier

Cindy M. said:
Hi Ogier,
The trick is in understanding that Ranges in a Table include the table
structure, i.e. End-of-cell and end-of-row markers. So if you pick up a cell's
range:
Set rng cell = tbl.rows(1).Range

You should then collapse that range to its starting pont
rngCell.Collapse(wdCollapseStart)

Doing this puts the "insertion point" at the start of the cell (think blinking
cursor), leaving out the end-of-cell structural elements.

Indeed, that did the trick! And now that you mention it, I remember having
read about it in your book. I have now found it in your "Achtung" on page 357!

Thanks again, wieder was dazugelernt.
Best wishes
Holger
 

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