Number of manual page breaks per section

A

andreas

Dear Experts:

I would like a macro to retrieve the following information when run
against a document:

- Total number of manual page breaks (Ctrl + Enter)
- Number of page breaks per section, such as Section 2 (5 page
breaks); Section 4 (3 page breaks) etc.

The information should be displayed in a msgBox.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
 
D

Doug Robbins - Word MVP

Use:

Dim i As Long, j As Long
Dim msg As String
msg = ""
With ActiveDocument
For i = 1 To .Sections.Count
Selection.Find.ClearFormatting
j = 0
With .Sections(i).Range.Find
Do While .Execute(FindText:=Chr(12), Forward:=True,
Wrap:=wdFindStop) = True
j = j + 1
Loop
End With
msg = msg & "There are " & j & " hard page breaks in Section " & i &
"; "
Next i
End With
msg = Left(msg, Len(msg) - 2) & "."
MsgBox msg
 
T

Tony Jollans

That won't work - Chr(12) isn't just a page break - try this instead ...

Dim j As Long
Dim msg As String
msg = ""
Dim S As Section, R As Range
For Each S In ActiveDocument.Sections
j = 0
Set R = S.Range
Do While R.Find.Execute(FindText:="^m") = True
If R.Start > S.Range.End Then Exit Do
j = j + 1
Loop
msg = msg & "There are " & j & " hard page breaks in Section " & S.Index
& "; "
Next
msg = Left(msg, Len(msg) - 2) & "."
MsgBox msg

--
Enjoy,
Tony

www.WordArticles.com

Doug Robbins - Word MVP said:
Use:

Dim i As Long, j As Long
Dim msg As String
msg = ""
With ActiveDocument
For i = 1 To .Sections.Count
Selection.Find.ClearFormatting
j = 0
With .Sections(i).Range.Find
Do While .Execute(FindText:=Chr(12), Forward:=True,
Wrap:=wdFindStop) = True
j = j + 1
Loop
End With
msg = msg & "There are " & j & " hard page breaks in Section " & i
& "; "
Next i
End With
msg = Left(msg, Len(msg) - 2) & "."
MsgBox msg


--
Hope this helps,

Doug Robbins - Word MVP
dkr[atsymbol]mvps[dot]org

andreas said:
Dear Experts:

I would like a macro to retrieve the following information when run
against a document:

- Total number of manual page breaks (Ctrl + Enter)
- Number of page breaks per section, such as Section 2 (5 page
breaks); Section 4 (3 page breaks) etc.

The information should be displayed in a msgBox.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
 
A

andreas

That won't work - Chr(12) isn't just a page break - try this instead ...

Dim j As Long
Dim msg As String
msg = ""
Dim S As Section, R As Range
For Each S In ActiveDocument.Sections
    j = 0
    Set R = S.Range
    Do While R.Find.Execute(FindText:="^m") = True
        If R.Start > S.Range.End Then Exit Do
        j = j + 1
    Loop
    msg = msg & "There are " & j & " hard page breaks in Section " & S.Index
& "; "
Next
msg = Left(msg, Len(msg) - 2) & "."
MsgBox msg

--
Enjoy,
Tony

 www.WordArticles.com



Dim i As Long, j As Long
Dim msg As String
msg = ""
With ActiveDocument
   For i = 1 To .Sections.Count
       Selection.Find.ClearFormatting
       j = 0
       With .Sections(i).Range.Find
           Do While .Execute(FindText:=Chr(12), Forward:=True,
Wrap:=wdFindStop) = True
               j = j + 1
           Loop
       End With
       msg = msg & "There are " & j & " hard page breaks in Section " & i
& "; "
   Next i
End With
msg = Left(msg, Len(msg) - 2) & "."
MsgBox msg
Doug Robbins - Word MVP
dkr[atsymbol]mvps[dot]org

- Zitierten Text anzeigen -

Hi Doug, hi Tony,

thank you very much for your great support. Yes, Tony was right, the
search for Chr(12) gave erroneous results. Doug, nevertheless, your
code was almost correct and I appreciate the time taken. You have
helped me so much with other coding in the past.

Thank you again to you two. 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