A
andreas
Dear Experts:
below macros deletes all existing manual page breaks before paragraphs
formatted with built-in heading style 1.
The macro works fine.
I now would like to count the number of manual page breaks deleted
immediately before headings level 1 and display it in a Message Box.
How do I have to re-write the code to achieve this?
Help is much appreciated. Thank you very much in advance. Regards,
Andreas
Sub DelPageBreakBefHd1()
Dim rgeDoc As Range
Set rgeDoc = ActiveDocument.Range
If MsgBox("Deletion of all manual page breaks before headings level
1?'" & vbCrLf & _
"Would you like to continue?", vbYesNo + vbQuestion, "Deletion of
manual page breaks before Heading 1") = vbNo Then
Exit Sub
End If
Set rng = ActiveDocument.Range
With rng.Find
.Style = ActiveDocument.Styles(wdStyleHeading1)
' Search from Beginning of Document
.Forward = True
'Find only one occurrence
.Wrap = wdFindStop
.Format = True
If Not .Execute() Then
MsgBox "No paragraph formatted with Heading level 1 style
in current document." & vbCrLf & vbCrLf & _
"Please make sure that at least one heading is formatted
with 'Heading 1" & vbCrLf & vbCrLf & _
"Macro will exit!", vbCritical, "no paragraph found with
heading style 'Heading 1'"
Exit Sub
End If
End With
Set rngStory = ActiveDocument.StoryRanges(wdMainTextStory)
With rngStory.Find
.Text = "^m"
.MatchWildcards = False
' .Replacement.Text = ""
While .Execute
i = i + 1
rngStory.Collapse wdCollapseEnd
Wend
End With
' Loop Until rngStory Is Nothing
If i = 0 Then
MsgBox "No Manual Page Breaks found!", vbCritical, "No Manual Page
Breaks"
End If
Exit Sub
With rgeDoc.Find
.Text = "^m"
While .Execute
If .Parent.Next.Paragraphs(1).Range.Style = _
ActiveDocument.Styles(wdStyleHeading1) Then
.Parent.Delete
End If
Wend
End With
Application.Browser.Target = wdBrowsePage
End Sub
below macros deletes all existing manual page breaks before paragraphs
formatted with built-in heading style 1.
The macro works fine.
I now would like to count the number of manual page breaks deleted
immediately before headings level 1 and display it in a Message Box.
How do I have to re-write the code to achieve this?
Help is much appreciated. Thank you very much in advance. Regards,
Andreas
Sub DelPageBreakBefHd1()
Dim rgeDoc As Range
Set rgeDoc = ActiveDocument.Range
If MsgBox("Deletion of all manual page breaks before headings level
1?'" & vbCrLf & _
"Would you like to continue?", vbYesNo + vbQuestion, "Deletion of
manual page breaks before Heading 1") = vbNo Then
Exit Sub
End If
Set rng = ActiveDocument.Range
With rng.Find
.Style = ActiveDocument.Styles(wdStyleHeading1)
' Search from Beginning of Document
.Forward = True
'Find only one occurrence
.Wrap = wdFindStop
.Format = True
If Not .Execute() Then
MsgBox "No paragraph formatted with Heading level 1 style
in current document." & vbCrLf & vbCrLf & _
"Please make sure that at least one heading is formatted
with 'Heading 1" & vbCrLf & vbCrLf & _
"Macro will exit!", vbCritical, "no paragraph found with
heading style 'Heading 1'"
Exit Sub
End If
End With
Set rngStory = ActiveDocument.StoryRanges(wdMainTextStory)
With rngStory.Find
.Text = "^m"
.MatchWildcards = False
' .Replacement.Text = ""
While .Execute
i = i + 1
rngStory.Collapse wdCollapseEnd
Wend
End With
' Loop Until rngStory Is Nothing
If i = 0 Then
MsgBox "No Manual Page Breaks found!", vbCritical, "No Manual Page
Breaks"
End If
Exit Sub
With rgeDoc.Find
.Text = "^m"
While .Execute
If .Parent.Next.Paragraphs(1).Range.Style = _
ActiveDocument.Styles(wdStyleHeading1) Then
.Parent.Delete
End If
Wend
End With
Application.Browser.Target = wdBrowsePage
End Sub