Find Style: Added Format

J

John in Toronto

I am relatively new to VBA.

I want my macro to search for "Heading 2" but each time it runs additional
formatting is added, and the style is not found. I recorded the macro, and
left the default settings:
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 2")
Selection.Find.ParagraphFormat.Borders.Shadow = False
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute

When I look at the find dialog, it shows:
Style: Heading 2, Border: Top: (Single solid line, Auto, 0.75 pt Line
Width)...

I have tried using different templates too.

Any help would be appreciated.

Thanks

John
 
H

Helmut Weber

Hi John,

Sub test66678()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
.Style = "Heading 2"
While .Execute
rDcm.Select ' for testing
' what now?
Wend
End With
End Sub

Depending on what you do to the found range,
you have to define the range "rDcm" anew.

--

Gruß

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
J

John in Toronto

Many thanks, Helmut.

I can follow what you did, and understand how it works. Is it worth while to
explain why a simple search was not able to do this?

Thanks again,

John
 
H

Helmut Weber

Hi John,

the macro recorder records everything,
kind of overspecification (!),
a term well known in linguistics.





--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 

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