Converting a word

L

LEU

I have the following macro that looks for a word in the document and replaces
it if it’s in a Heading style. The problem is it does not get all the words I
am looking for. How would I rewrite it to get all the words except if the
word is in a table, textbox or a graph?

Dim SearchRange As Range
Set SearchRange = ActiveDocument.Range
With SearchRange.Find
..Text = InputBox("Word to Bold and Cap:")
..MatchWholeWord = True
While .Execute
Select Case SearchRange.Style
Case "Heading 1", "Heading 2", "Heading 3", "Heading 4", "Heading 5",
"Heading 6", "Heading 7"
SearchRange.Font.AllCaps = True
SearchRange.Font.Bold = True
Case Else
'Do Nothing
End Select
Wend
End With

LEU
 
R

Russ

You should use:
While .Execute = True
To do stuff only if something found.

You code is asking for a word or phrase to search for. If found, it checks
whether it is one of the heading styles. If it is a heading style, you try
to make Bold (already bold, isn't it?) and uppercase.

Did you want to expand to the whole paragraph, before doing that?
SearchRange.Expand Unit:=wdParagraph

But if you mess with the SearchRange range with expansion, then you should
reset it before the Wend with:
SearchRange.SetRange = Start:=SearchRange.End, End:=ActiveDocument.End

If a chart is in a frame, then this might be enough:

If Not SearchRange.Information(wdWithInTable) Or _
SearchRange.Information(wdFrameIsSelected) Then
....Select Case ...
End If
 
L

LEU

Hi Russ,

That work great. I did not know about the 'If Not SearchRange' statement.
It's great to learn new things.

Thank you for all your help again.
LEU
 

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