Insert text in table cell with formatting?

M

mrbenit

Hi everyone,

I am trying to add, conditionally, text with formating to different
cells of a word table.

I am managing with Range object like:

Set oRange = objTable.Rows(Position).Cells(2).Range
oRange.InsertAfter "Name1"
objTable.Rows(Position).Cells(2).Range.InsertParagraphAfter
...

but I do not know anything about setting font, underline, italics,
etc.

I would need to add:

Name1 (in bold)
Name2 (regular)
....
Name5 (in italics)
....

everything in one cell

Any suggestions?

Thanks.
Miguel Ruiz.
 
J

JGM

Hi Miguel

For something simple like this you have two options:

1) Look for Font, Bold, etc. in the IDE help (F2 when in the IDE
window to browse for objects and their properties/methods) and look
at the sample code.
2) Write some text in a document, Start the macro recorder, change
the font attributes (Bold, Italic, Size, and so on) and look at the
resulting
code the recorder has generated. You will need to trim it down to only
what you need or if you see a property that you think would be helpful
and would like to know more about it, double click on it to select it,
then hit F1 to get the help topics and the sample codes.

Option 2 seems like a pain in the provrerbial behind, but it is very useful
and does not take that long when you are used to it. Use that only for
simple
stuff (font or paragarpah attributes, adding a line, moving the cursor
around,
closing, saving, etc.) otherwise the code generated may be a bit to twisted
(and even unworkable when you run the macro afterwards)
to be of any value if you are trying to learn something.

HTH
Cheers
 
R

Robert

Here is a sample of some code I'm using to fill out a table with data
from a recordset:

Set rng = ActiveDocument.Bookmarks(strBookmark).Range

Do Until rs.EOF
rng.Select
Selection.SelectCell
Selection.Delete
Selection.Font.Name = "Times New Roman"
Selection.Font.Size = 9
Selection.Font.Bold = False
rng.InsertAfter varHeaders(0) & NL ' NL = vbcrlf
rng.Move unit:=wdWord, Count:=1
rng.InsertAfter rs.Fields("PrevEmployer").Value
rng.Font.Name = "Arial"
rng.Font.Size = 9
rng.Font.Bold = False

... similar code for other cells deleted

rng.Move unit:=wdCell, Count:=1
rs.MoveNext
Loop

What it does is to first delete whatever header text is in the cell,
then replace it with text from an array, varHeaders(), format that as
Times Roman, then insert the text from the recordset and format that
as Arial.

The interesting thing I found here was that it was necessary to format
the array header text before inserting it, but to format the remaining
text after insertion. Also, I had to use the Selection object for the
first, but the range object for the second. It took awhile to work
that out.

Hope this helps you get started.

Robert
 

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