Number of pages in a section?

J

Jeffrey Bradshaw

Is there a way in VB.NET to find out how many pages are in a section? I
assume I could select the section and then count the number of times the
bookmark \page appears but I was wondering if there is an easier way.

TIA - Jeffrey
 
J

Jay Freedman

Jeffrey Bradshaw said:
Is there a way in VB.NET to find out how many pages are in a section? I
assume I could select the section and then count the number of times the
bookmark \page appears but I was wondering if there is an easier way.
Hi, Jeffrey,

This is in VBA, but the conversion to VB.Net isn't too difficult.

Public Sub SectionPages()
Dim oRg As Range
Dim StartPg As Long, EndPg As Long

Set oRg = Selection.Sections(1).Range
With oRg
.Collapse wdCollapseStart
StartPg = .Information(wdActiveEndPageNumber)
End With

Set oRg = Selection.Sections(1).Range
With oRg
.Collapse wdCollapseEnd
' it's now at the first char of
' the next section -- move back
.Move wdCharacter, -1
EndPg = .Information(wdActiveEndPageNumber)
End With

MsgBox "Section contains " & _
EndPg - StartPg + 1 & " page(s)" & vbCr & _
"(" & StartPg & "-" & EndPg & ")"
End Sub

In VB.Net, you need to know that the wdXX constants are
Word.WdInformation.wdActiveEndPageNumber and Word.WdUnits.wdCharacter.
Also, I suspect you want to interrogate some specific section number
nSection, so set oRg to wdDoc.Sections.Item(nSection).Range instead of
Selection.Sections(1).Range.
 
L

Lars-Eric Gisslén

Jeffrey,

In addition to Jay's suggestion you could also try this code
' For a specific section
nPages = ActiveDocument.Sections(1).Range.ComputeStatistics(2)
' The section the insertion point is in
nPages = Selection.Range.Sections(1).Range.ComputeStatistics(2)

--
Regards,
Lars-Eric Gisslén
**Reply only to this Newsgroup**
Any reply as private email will only generate
a spam complaint to your ISP!
 
J

Jay Freedman

Lars-Eric Gisslén said:
Jeffrey,

In addition to Jay's suggestion you could also try this code
' For a specific section
nPages = ActiveDocument.Sections(1).Range.ComputeStatistics(2)
' The section the insertion point is in
nPages = Selection.Range.Sections(1).Range.ComputeStatistics(2)

Thanks, Lars-Eric. I had forgotten that function even existed.
 
L

Larry

It's the old problem with Word--it's so big that it's impossible for any
human being to remember everything that he already knows about it. :)

On the ComputeStatistics method, one should be aware that this will not
work correctly on a selection object in Word 97, and perhaps in Word
2000 as well. (I know the problem is fixed in Word 2002.) Thus I just
tried this on a section in a document in Word 97 and got incorrect
results:

MsgBox
Selection.Range.Sections(1).Range.ComputeStatistics(wdStatisticPages)

I ran into this problem earlier in trying to get wdStatisticsWords to
work on a selection.

Larry
 
L

Lars-Eric Gisslén

Larry,

I tested with Word 2000 SP-3 and it seems to work correctly here.

--
Regards,
Lars-Eric Gisslén
**Reply only to this Newsgroup**
Any reply as private email will only generate
a spam complaint to your ISP!
 

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