VBA-How to bold some text in a table cell

R

RobGMiller

Word 2003

How to select only a portion of all text in a table cell and bold it
programatically using VBA?

the Cell was filled programtically in the following manner

Doc.Tables(1).Cell(10, 1).Range.InsertAfter "some text - bolded text"


Thanks,
 
D

David Sisson

Here's one way.

Sub HalfBold()
'Assumes the divider is a dash with one space after it.
Dim Rng As Range
Dim Doc As Document
Set Doc = ActiveDocument

Doc.Tables(1).Cell(10, 1).Range.InsertAfter "some text - bolded text"
Set Rng = Doc.Tables(1).Cell(10, 1).Range
'Remove end-of-cellmarker
Rng.MoveEnd wdCharacter, -1
'Move the start of the range just past the space.
Rng.MoveStart wdCharacter, InStr(Rng.Text, " - ")
'Bold it.
Rng.Font.Bold = True

End Sub

But as far as I know, you can't bold a literal string until it's part
of the document.
 
R

RobGMiller

Thanks for the reply David.

The suggested code bolds the whole line.

If this was being done manually, the section of the line to be bolded would
have to be highlighted. Is there something in the suggested code that is
supposed to highlight the text or at least define the end of the section to
be bolded.

What if the section to bold is not at the end of the line.

I've tried using a macro to point the way
Here is the code.

Selection.MoveRight Unit:=wdCharacter, Count:=8, Extend:=wdExtend
Selection.Font.Bold = wdToggle

As you can see, when I was moving the cursor using the arror keys I was
holding the shift key down to highlight. There appears to be nothing in this
code to record the shift key resulting in a highlighted condition.

I'm not sure what the extend: parameter means but If I try to add that to
the VBA code it is not recognized.
 
D

Doug Robbins - Word MVP

You need to be able to specify exactly what it is that you want bolded in
some way. What is there about it that might be used as a way of locating
it.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

RobGMiller

Perhaps a different approach might be to place a bold indicator in the text
when it is applied to the page.

IE: Selection.TypeText Text:="Text" & BoldStart & "Bolded Text" & Boldend

There must be a character indicating the bold condition in the text under
normal circumstances.
 
R

RobGMiller

I found a way to do it when there is no table cell involved.

Selection.TypeText Text:= "Normal Text"
Selection.Font.Bold = True
Selection.TypeText Text:= "Bold Text" & vbCrlf
Selection.Font.Bold = False

Still not sure how to do it within a table cell.
 
R

Russ

It should work the same inside a cell. Use whatever code you need to
position the cursor. Carefully not selecting the cell marker at end of cell.

Using Selection:
Doc.Tables(1).Cell(10, 1).Range.InsertAfter "some plain text."
Doc.Tables(1).Cell(10, 1).Range.Characters.Last.Previous.Select
Selection.Collapse (wdCollapseEnd)
Selection.Font.Bold = True
Selection.TypeText " Make me Bold!"
Selection.Font.Bold = False

Using Range and not positioning cursor:
Dim aRange As Range
Set aRange = Doc.Tables(1).Cell(10, 1).Range
aRange.InsertAfter "some plain text."
aRange.Collapse Direction:=wdCollapseEnd
aRange.MoveEnd Unit:=wdCharacter, Count:=-1
aRange.Text = " Make me Bold!"
aRange.Font.Bold = True
 

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