PDA

View Full Version : Solved: Using Find, Make Certain Text Bold



Belch
09-03-2007, 08:01 AM
Hi all,

I have some text which is input to a String variable. This String is then output to a cell in a table. This is repeated many times, being added to a new row in the table each time.

Some text needs to be made bold - sometimes a single word, sometimes a sentence. Each word that needs to be made bold has a "%%" before it - this is the only way I could think to mark which words to make bold. So for example a word would appear as "%%hello".
At the moment I have the following code:

With Selection
.Find.ClearFormatting
.Find.Text = "%%"
Do While .Find.Execute(Forward:=True, Wrap:=wdFindContinue) = True
.Expand (wdWord)
.Font.Bold = True
Loop
End With

This finds the %% text ok, and makes the %% bold, but as you can see I'm trying to expand the selection to the word attached to the %% before setting bold to true. However it isn't expanding the selection.

Any ideas? Thanks in advance,

mdmackillop
09-03-2007, 03:15 PM
With Selection
.HomeKey Unit:=wdStory
.Find.Text = "%%* "
Do While .Find.Execute(Forward:=True, Wrap:=wdFindStop) = True
.Font.Bold = True
Loop
.HomeKey Unit:=wdStory
End With

fumei
09-03-2007, 06:49 PM
And, as usual, I have to comment it is better to not use Selection. To use a Range - for just a word:Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.ClearFormatting
.MatchWildcards = True
.Text = "%%* "
Do While (.Execute(Forward:=True) = True) = True
r.Font.Bold = True
Loop
End With

And to use a range for a sentence:Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.ClearFormatting
.MatchWildcards = True
.Text = "%%* "
Do While (.Execute(Forward:=True) = True) = True
With r
.Expand Unit:=wdSentence
.Font.Bold = True
.Collapse Direction:=wdCollapseEnd
End With
Loop
End WithThe CollapseEnd is critical, otherwise it will start an endless loop.

Also, it is better to explicitly make MatchWildCards = True. If it is not True, then "%%* " can fail.

fumei
09-03-2007, 06:54 PM
And, BTW, if you do NOT want to use wildcards, they are, in fact, not strictly needed.Dim r As Range
Set r = ActiveDocument.Range
With r.Find
Do While (.Execute(FindText:="%%", Forward:=True) = True) = True
With r
.Expand Unit:=wdSentence
.Font.Bold = True
.Collapse Direction:=wdCollapseEnd
End With
Loop
End With

mdmackillop
09-04-2007, 12:05 AM
Thanks Gerry :whip:

fumei
09-04-2007, 12:38 AM
You're welcome.

I seem to be on a roll......ummmmm, rant?

Belch
09-04-2007, 02:41 AM
Thanks for the advice, I took your idea of using a range and the Do Loop and came up with this:


Dim r As Range
Set r = ActiveDocument.Range
With r.Find
.ClearFormatting
.MatchWildcards = True
.Text = "%%*"
Do While (.Execute(Forward:=True) = True) = True
r.MoveEnd Unit:=wdWord
r.Font.Bold = True
r.Collapse Direction:=wdCollapseEnd
Loop
End With


I removed the space from the .Text value as it wasn't finding lines with just one word on them. I then had to use the MoveEnd method as it was then just selecting the "%%" bit of the line.

Also, I don't fully understand the Do While line - I can understand the Forward=True and .Execute(..) = True, but how come there is a third =True?

fumei
09-04-2007, 12:36 PM
You can, in fact, for most purposes not use it...

Belch
09-05-2007, 12:27 AM
I think I'll leave it in anyway - if it ain't broke, don't fix it. :)