Microsoft KB Archive/293861

= How to use Word automation to count the number of pages in each section of a document =

Article ID: 293861

Article Last Modified on 7/5/2007

-

APPLIES TO


 * Microsoft Office Word 2007
 * Microsoft Office Word 2003
 * Microsoft Word 2002 Standard Edition
 * Microsoft Word 2000 Standard Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Foundation Class Library 4.2

-



This article was previously published under Q293861



SUMMARY
This article describes how you can use automation with Word to determine the number of pages in each section of a document.



MORE INFORMATION
The following sample code uses a document that is saved to C:\Mydoc.doc. For testing the sample code, either create a new document with multiple sections and multiple pages and save it as C:\Mydoc.doc, or change the document path in the code to reference one of your existing Word documents.

NOTE: The sample code assumes that a section break forces a new page break and that each page contains no more than one section. Therefore, when you insert section breaks while creating the C:\Mydoc.doc Word document for testing the sample code, you should choose Next Page as the Section Break type.

Visual Basic Sample
 In Visual Basic, create a new Standard EXE project. Form1 is created by default.  Add a command button to Form1, and add the following code to the button's Click event: Dim oApp As Object Dim oDoc As Object Dim oTbl As Object 'Start Word and open the document. Set oApp = CreateObject(&quot;Word.Application&quot;) 'For Word 2007, change the path to &quot;c:\mydoc.docx&quot; Set oDoc = oApp.Documents.Open(&quot;c:\mydoc.doc&quot;) 'Repaginate the document. oDoc.Repaginate 'Iterate each section in the document to retrieve the end page of the 'document and compute the page count in that section. The results are 'displayed in the Immediate window. Dim oSec As Object Dim nStartPg As Integer, nEndPg As Integer, nSecPages As Integer Dim NumSections As Integer NumSections = oDoc.Sections.Count nStartPg = 1 For Each oSec In oDoc.Sections nEndPg = oSec.Range.Information(3) - 1 'wdActiveEndPageNumber=3 'Account for the last page. If oSec.Index = NumSections Then nEndPg = nEndPg + 1 nSecPages = nEndPg - nStartPg + 1 Debug.Print &quot;Section &quot; & oSec.Index & &quot; --&quot;, _ &quot;StartPage: &quot; & nStartPg, _ &quot;EndPage: &quot; & nEndPg, _ &quot;TotalPages: &quot; & nSecPages nStartPg = nEndPg + 1 Next 'Close the document without saving changes and quit Word. oDoc.Close False oApp.Quit  Press F5 to run the application, and click the button on the form. The code displays the page count for each section in the Immediate window.

MFC Sample
 Follow steps 1 through 12 in the following article in the Microsoft Knowledge Base to create a sample project that uses the IDispatch interfaces and member functions that are defined in the MSWord9.olb type library:

178749 How to create an automation project using MFC and a type library

 Note You must remove the Excel automation library.

At the top of AutoProjectDlg.cpp, add one of the following lines:   In Word 2002 and in later versions of Word, add the following line:  In Word 2000, add the following line: </li>  Add the following code to CAutoProjectDlg::OnRun in AutoProjectDlg.cpp. COleVariant vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR); //Start Word. _Application oWord; oWord.CreateDispatch(&quot;Word.Application&quot;); oWord.SetScreenUpdating(FALSE);
 * 1) include &quot;MSWord.h&quot; </li>
 * 1) include &quot;MSWord9.h&quot; </li></ul>

//Open the document. Documents oDocs = oWord.GetDocuments; // For Word 2007, use: // _Document oDoc = oDocs.Open(COleVariant(&quot;c:\\mydoc.docx&quot;), //        vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt, vOpt, vOpt,vOpt,vOpt,vOpt,vOpt); _Document oDoc = oDocs.Open(COleVariant(&quot;c:\\mydoc.doc&quot;),          vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt, vOpt, vOpt,vOpt,vOpt,vOpt,vOpt);

//Repaginate the document. oDoc.Repaginate;

//Iterate the collection of sections in the document to retrieve the page //count for each section. Sections oSecs = oDoc.GetSections; long NumSections = oSecs.GetCount; long i; long StartPage=1; //Section start page. long EndPage=0;  //Section end page. long NumPages=0; //Number of pages in the section. for(i=1;i<=NumSections;i++) {   Section oSec = oSecs.Item(i); Range oSecRange = oSec.GetRange; VARIANT vInfo = oSecRange.GetInformation(3L);//wdActiveEndPageNumber=3 //If oSec.Index = NumSections Then nEndPg = nEndPg + 1 if(oSec.GetIndex== NumSections) {EndPage++;} EndPage = vInfo.lVal-1; if(i==NumSections) {EndPage++;} //Account for the last section. NumPages = EndPage - StartPage +1; char buf[5]; sprintf(buf,&quot;%d&quot;, NumPages); TRACE1(&quot;Section %d\n&quot;, oSec.GetIndex); TRACE3(&quot;  StartPage: %d  EndPage: %d   TotalPages: %d\n&quot;,           StartPage, EndPage, NumPages); StartPage = EndPage + 1; }

//Close the document without saving the changes, and then exit Word. oDoc.Close(COleVariant((short)false), vOpt, vOpt); oWord.Quit(COleVariant((short)false), vOpt, vOpt); </li> Compile and run the project.</li> When the dialog box appears, click Run. The count results are displayed in the Debug window. You must drag the NumPages variable to the Debug window.</li></ol>

(c) Microsoft Corporation 2001, All Rights Reserved. Contributions by Lori B. Turner, Microsoft Corporation.

<div class="references_section">