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
Helmut Weber - 30 Nov 2007 20:50 GMT
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
John in Toronto - 30 Nov 2007 21:02 GMT
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
> Hi John,
>
[quoted text clipped - 20 lines]
>
> Vista Small Business, Office XP
Helmut Weber - 30 Nov 2007 21:39 GMT
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