Count the number of deleted manual page breaks

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
 
A

ARAHII

Andreas,

Your code demonstrates lots of code writing talent, so maybe I don't
understand your question, here's my suggestion.

In front of the last loop (e.g., before With rgeDoc.Find) insert
count = 0 'not necessary if local variable and only used below

Inside the if of the last loop (e.g., after .Parent.Delete) insert
count = count + 1

Before the end of the sub (e.g., after the End With of With rgeDoc.Find)
insert
MsgBox "number of manual page breaks deleted: " & count
 
D

Doug Robbins - Word MVP

Replace

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

with

i = 0
With rgeDoc.Find
.Text = "^m"
While .Execute
If .Parent.Next.Paragraphs(1).Range.Style = _
ActiveDocument.Styles(wdStyleHeading1) Then
.Parent.Delete
i = i + 1
End If
Wend
End With
Msgbox i & " manual page breaks have been deleted."

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
A

andreas

Replace

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

with

i = 0
With rgeDoc.Find
    .Text = "^m"
    While .Execute
        If .Parent.Next.Paragraphs(1).Range.Style = _
            ActiveDocument.Styles(wdStyleHeading1) Then
            .Parent.Delete
            i = i + 1
        End If
    Wend
End With
Msgbox i & " manual page breaks have been deleted."

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com



















- Show quoted text -

Hi Doug,

that's it! Thank you very much for your professional help. Regards,
Andreas
 
A

andreas

Andreas,

Your code demonstrates lots of code writing talent, so maybe I don't
understand your question, here's my suggestion.

In front of the last loop (e.g., before With rgeDoc.Find) insert
    count = 0  'not necessary if local variable and only used below

Inside the if of the last loop (e.g., after .Parent.Delete) insert
    count = count + 1

Before the end of the sub (e.g., after the End With of With rgeDoc.Find)
insert
    MsgBox "number of manual page breaks deleted: " & count

--
Art
___________________________________
Arthur R. Hendrickson, Jr.
DRS CONSOLIDATED CONTROLS, INC


















- Show quoted text -

Hi Arthur,

thank you very much for your swift and professional help. It works
just fine.

The praise for my skills in coding go right to the experts of this and
other forums that have contributed largely to the codes I am using. To
name a few: Greg Maxey, Doug Robbins, Mr. Jamieson, Macropod, Jay
Freedman and so forth.

Regards, Andreas
 

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