Count of pages and section number for odd section breaks

A

andreas

Dear Experts:

Is it possible to retrieve the section number of the sections with odd
section breaks along with their number of pages? The data should be
shown in a MsgBox:

Example:

3 (Three) Odd Section Breaks detected: Section No. 4 (5 pages);
Section No. 7 (3 pages); Section No. 10 (8 pages).

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

Regards, Andreas
 
J

Jay Freedman

Dear Experts:

Is it possible to retrieve the section number of the sections with odd
section breaks along with their number of pages? The data should be
shown in a MsgBox:

Example:

3 (Three) Odd Section Breaks detected: Section No. 4 (5 pages);
Section No. 7 (3 pages); Section No. 10 (8 pages).

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

Regards, Andreas

Loop through all of the sections, collecting information about only
the ones that start with an odd section break:

Sub OddBreakInfo()
Dim msg As String
Dim sec As Section
Dim cntr As Integer ' index of sec
Dim odds As Integer ' how many odd-start sections
Dim pgs As Integer ' page count of section
Dim rng As Range

For Each sec In ActiveDocument.Sections
cntr = cntr + 1
If sec.PageSetup.SectionStart = wdSectionOddPage Then
' increment count of odd-start sections
odds = odds + 1

' calculate number of pages in section:
' number of last page minus number of first page
' plus 1
Set rng = sec.Range
rng.Collapse wdCollapseStart
pgs = sec.Range.Information(wdActiveEndPageNumber) _
- rng.Information(wdActiveEndPageNumber) + 1

' update message string
msg = msg & "Section No. " & cntr & _
" (" & pgs & " pages)" & vbCr
End If
Next

msg = odds & " Odd Section Breaks detected" & vbCr & msg
MsgBox msg
End Sub
 
A

andreas

Loop through all of the sections, collecting information about only
the ones that start with an odd section break:

Sub OddBreakInfo()
    Dim msg As String
    Dim sec As Section
    Dim cntr As Integer  ' index of sec
    Dim odds As Integer  ' how many odd-start sections
    Dim pgs As Integer   ' page count of section
    Dim rng As Range

    For Each sec In ActiveDocument.Sections
        cntr = cntr + 1
        If sec.PageSetup.SectionStart = wdSectionOddPage Then
            ' increment count of odd-start sections
            odds = odds + 1

            ' calculate number of pages in section:
            ' number of last page minus number of first page
            ' plus 1
            Set rng = sec.Range
            rng.Collapse wdCollapseStart
            pgs = sec.Range.Information(wdActiveEndPageNumber) _
                - rng.Information(wdActiveEndPageNumber) + 1

            ' update message string
            msg = msg & "Section No. " & cntr & _
                " (" & pgs & " pages)" & vbCr
        End If
    Next

    msg = odds & " Odd Section Breaks detected" & vbCr & msg
    MsgBox msg
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroupso all may benefit.

Hi Jay,

very nice piece of coding. Thank you very much for your terrific
support.

There is one thing, I would like to ask you concerning the code. Would
it be possible to retrieve only the odd-page section breaks with an
odd count of pages?

Help is much appreciated. Thank you very much in advance. Regards,
Andreas
 
J

Jay Freedman

[snip]

Hi Jay,

very nice piece of coding. Thank you very much for your terrific
support.

There is one thing, I would like to ask you concerning the code. Would
it be possible to retrieve only the odd-page section breaks with an
odd count of pages?

Help is much appreciated. Thank you very much in advance. Regards,
Andreas

Hi Andreas,

Mostly it just takes a little rearrangement of the code so the page
count is calculated first; then one more If statement to check for an
odd number of pages.

Sub OddBreakInfo()
Dim msg As String
Dim sec As Section
Dim cntr As Integer ' index of sec
Dim odds As Integer ' how many odd-start sections
Dim pgs As Integer ' page count of section
Dim rng As Range

For Each sec In ActiveDocument.Sections
cntr = cntr + 1
If sec.PageSetup.SectionStart = wdSectionOddPage Then

' calculate number of pages in section:
' number of last page minus number of first page
' plus 1
Set rng = sec.Range
rng.Collapse wdCollapseStart
pgs = sec.Range.Information(wdActiveEndPageNumber) _
- rng.Information(wdActiveEndPageNumber) + 1

If pgs Mod 2 = 1 Then
' increment count of odd-start sections
odds = odds + 1

' update message string
msg = msg & "Section No. " & cntr & _
" (" & pgs & " pages)" & vbCr
End If
End If
Next

msg = odds & _
" Odd Section Breaks with odd page count detected" _
& vbCr & msg
MsgBox msg
End Sub
 
A

andreas

Is it possible to retrieve the section number of the sections with odd
section breaks along with their number of pages? The data should be
shown in a MsgBox:
[snip]



Hi Jay,
very nice piece of coding. Thank you very much for your terrific
support.
There is one thing, I would like to ask you concerning the code. Would
it be possible to retrieve only the odd-page section breaks with an
odd count of pages?
Help is much appreciated. Thank you very much in advance. Regards,
Andreas

Hi Andreas,

Mostly it just takes a little rearrangement of the code so the page
count is calculated first; then one more If statement to check for an
odd number of pages.

Sub OddBreakInfo()
    Dim msg As String
    Dim sec As Section
    Dim cntr As Integer  ' index of sec
    Dim odds As Integer  ' how many odd-start sections
    Dim pgs As Integer   ' page count of section
    Dim rng As Range

    For Each sec In ActiveDocument.Sections
        cntr = cntr + 1
        If sec.PageSetup.SectionStart = wdSectionOddPage Then

            ' calculate number of pages in section:
            ' number of last page minus number of first page
            ' plus 1
            Set rng = sec.Range
            rng.Collapse wdCollapseStart
            pgs = sec.Range.Information(wdActiveEndPageNumber) _
                - rng.Information(wdActiveEndPageNumber) + 1

            If pgs Mod 2 = 1 Then
                ' increment count of odd-start sections
                odds = odds + 1

                ' update message string
                msg = msg & "Section No. " & cntr & _
                    " (" & pgs & " pages)" & vbCr
            End If
        End If
    Next

    msg = odds & _
        " Odd Section Breaks with odd page count detected" _
        & vbCr & msg
    MsgBox msg
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ:http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroupso all may benefit.

Hi Jay,
works like a charm. Thank you very much for your great support.

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