Find and highlight some occurences in a doc

L

Lisa

Hello,

I would like to look for a specific type of formatting , e.g. "my_style" +
underlined, in a word doc and would like to highlight it in light grey in
addition.
I tried the following but I am a bit stuck, so any help would be much
appreciated:


With rngTemp.Find
.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Style = "my_style"

****
at this point, how to add the request that the word should be undelined?

****
While .Execute
Selection.Range.HighlightColorIndex = wdGray25
rngTemp.Collapse wdCollapseEnd
Wend

*****
this was my (weak!) attempt to select the word and highlight it accordingly
but this definitely does not work...

****

End With

Many Thanks in advance and happy new year in advance,

Lisa
 
L

Lisa

Ok, I think I got it (by the way why are there so few colors for the
highlight in comparison to the fonts?):


With rngTemp.Find
.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Style = "my_style"

***
.Font.Underline = True
***

While .Execute

***
rngTemp.HighlightColorIndex = wdGray25
***

rngTemp.Collapse wdCollapseEnd
Wend
 
J

Jay Freedman

at this point, how to add the request that the word should be undelined?

.Font.Underline = wdUnderlineSingle

Also, the line that sets the highlighting _must_ refer to rngTemp and not to
Selection. The reason is that the .Find.Execute moves rngTemp but it never moves
the Selection; so the code as you wrote it would highlight only the place where
the user left the selection when the macro started.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
L

Lisa

Thx Jay,

I assume wdUnderlineSingle is more precise than putting simply true.

Best regards, Lisa
 
J

Jay Freedman

Now you're getting into one of the murkier areas of VBA. :)

If you look at the Help topic for the Underline property, it says the data type
of that property is WdUnderline. That's a specific set of values (technically
called an "enumeration") defined as named constants with integer values. For
example, wdUnderlineNone has the value 0, wdUnderlineSingle has the value 1, and
wdUnderlineDouble has the value 3. (To see all the values, in the VBA editor
press F2 to open the Object Browser, type WdUnderline in the search box, and
click the binoculars button. Click any name to see the numeric value at the
bottom.)

The numeric value of the Boolean constant True is -1, which isn't the value of
any of the WdUnderline constants. By some magic of internal programming in the
VBA engine, though, you're allowed to assign True to the Underline property and
it gets converted from -1 to 1 without any complaint about the value being
out of range.

So in that sense wdUnderlineSingle and True are equivalent, but it's good to
remember that they're not really the same thing. In some context other than
assignment, that difference might bite you. For example, if the cursor is in
some single-underlined text and you run the code

If Selection.Font.Underline = True Then
MsgBox "Underlined"
Else
MsgBox "Not underlined"
End If

the resulting message box will say "Not underlined". That's because 1 is not
equal to -1.
 
G

Gordon Bentley-Mix

A very concise and informative post Jay - as usual. ;-D
-----
Cheers!

Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
L

Lisa

Indeed, thx for the infos.
Regards, Lisa

Gordon Bentley-Mix said:
A very concise and informative post Jay - as usual. ;-D
-----
Cheers!

Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post
all
follow-ups to the newsgroup.
 

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