Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2007

Tip: Looking for answers? Try searching our database.

VBA-How to bold some text in a table cell

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RobGMiller - 26 Jul 2007 20:16 GMT
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,
Signature

RobGMiller

David Sisson - 26 Jul 2007 22:14 GMT
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.
RobGMiller - 26 Jul 2007 22:46 GMT
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.

Signature

RobGMiller

> Here's one way.
>
[quoted text clipped - 17 lines]
> But as far as I know, you can't bold a literal string until it's part
> of the document.
Doug Robbins - Word MVP - 28 Jul 2007 10:07 GMT
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.

Signature

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

> Thanks for the reply David.
>
[quoted text clipped - 44 lines]
>> But as far as I know, you can't bold a literal string until it's part
>> of the document.
RobGMiller - 28 Jul 2007 15:38 GMT
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.
Signature

RobGMiller

> 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
[quoted text clipped - 48 lines]
> >> But as far as I know, you can't bold a literal string until it's part
> >> of the document.
RobGMiller - 28 Jul 2007 16:30 GMT
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.
Signature

RobGMiller

> 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
[quoted text clipped - 48 lines]
> >> But as far as I know, you can't bold a literal string until it's part
> >> of the document.
Russ - 29 Jul 2007 03:52 GMT
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

> I found a way to do it when there is no table cell involved.
>
[quoted text clipped - 4 lines]
>
> Still not sure how to do it within a table cell.

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.