Log in

View Full Version : Macro to do a search within a search, then change paragraph properties



janaboo13
02-16-2015, 12:15 PM
Greetings Experts!

I need a macro that allows me to search for a style (Heading 1) and when found, move the cursor to the end of the line and search for the first Heading 2 style that follows. When that style is found, I want to change the .SpaceBefore to 0.

I can get this to work with only one search, but I'm lost with two searches and the loop so that I can search the entire document.

I've recorded the actions:


Sub Macro1()'
' Macro2 Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 1")
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchControl = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Heading 2")
With Selection.Find
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchControl = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
End With
Selection.Find.Execute
With Selection.ParagraphFormat
.SpaceBefore = 0
End With
End Sub

You can't know how I appreciate this forum and all the help that Greg Maxey and Graham Mayor (and others over the past couple of years) have given me, so any additional help would not go unappreciated.

Thanks! Jan

gmaxey
02-16-2015, 02:18 PM
Something like:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
Do
With oRng.Find
.Style = "Heading 1"
If .Execute Then
oRng.Collapse wdCollapseEnd
oRng.End = ActiveDocument.Range.End - 1
With oRng.Find
.Style = "Heading 2"
If .Execute Then
oRng.Paragraphs(1).SpaceBefore = 0
oRng.Collapse wdCollapseEnd
oRng.End = ActiveDocument.Range.End - 1
End If
End With
Else
Exit Do
End If
End With
Loop
End Sub

janaboo13
02-16-2015, 04:22 PM
Hi Greg!

Thank you so much for the quick response...works perfectly!!

I would love you to explain something to me so I can learn. There are two parts that I don't get...see the comment in red below.

The rest I think I understand, but if you want to further annotate, that would be awesome.



Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
Set oRng = ActiveDocument.Range
Do
With oRng.Find
.Style = "Heading 1"
If .Execute Then
oRng.Collapse wdCollapseEnd
oRng.End = ActiveDocument.Range.End - 1 'what does this do?
With oRng.Find
.Style = "Heading 2"
If .Execute Then
oRng.Paragraphs(1).SpaceBefore = 0
oRng.Collapse wdCollapseEnd
oRng.End = ActiveDocument.Range.End - 1 'what does this do?

End If
End With
Else
Exit Do
End If
End With
Loop End Sub


Thanks again!! Jan

gmaxey
02-16-2015, 06:40 PM
Jan

In practice that may not be required (the .End -1) part and the first instance probably not at all. Open a new document and enter 4 short lines of text (4 paragraphs). Apply Heading 1 to lines 1 & 3 and Heading 2 to lines 2 & 4.


Now remove the -1 from the two lines of code and run it. You should notice that the second Heading 2 isn't processed


Do you know why? Consider this. You would expect to find chocolates in a box of chocolates but you wouldn't find the box of chocolates in the box of chocolates. When the range of the thing searched for is the same as the search range it can't be found


See my Word's Fickle Find method