How To Use Word Automation to Count the Number of Pages in Each Section of a Document (293861)



The information in this article applies to:

  • Microsoft Office Word 2003
  • Microsoft Word 2002
  • Microsoft Word 2000
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual C++, 32-bit Professional Edition 6.0
  • The Microsoft Foundation Classes (MFC)

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

  1. In Visual Basic, create a new Standard EXE project. Form1 is created by default.
  2. 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("Word.Application")
        Set oDoc = oApp.Documents.Open("c:\mydoc.doc")
        
        '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 "Section " & oSec.Index & " --", _
                       "StartPage: " & nStartPg, _
                       "EndPage: " & nEndPg, _
                       "TotalPages: " & nSecPages
           nStartPg = nEndPg + 1
        Next
        
        'Close the document without saving changes and quit Word.
        oDoc.Close False
        oApp.Quit
    					
  3. 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

  1. 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

  2. At the top of AutoProjectDlg.cpp, add the following line:
    #include "MSWord9.h"
  3. Add the following code to CAutoProjectDlg::OnRun() in AutoProjectDlg.cpp:
    COleVariant vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
     
    //Start Word.
    _Application oWord;
    oWord.CreateDispatch("Word.Application");
    oWord.SetScreenUpdating(FALSE);
    
    //Open the document.
    Documents oDocs = oWord.GetDocuments();
    _Document oDoc = oDocs.Open(COleVariant("c:\\mydoc.doc"),
      	   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,"%d", NumPages);
    	TRACE1("Section %d\n", oSec.GetIndex());
    	TRACE3("   StartPage: %d  EndPage: %d   TotalPages: %d\n",
    		   StartPage, EndPage, NumPages);
    	StartPage = EndPage + 1;
    }
    
    //Close the document without saving changes and quit Word.
    oDoc.Close(COleVariant((short)false), vOpt, vOpt);	
    oWord.Quit(COleVariant((short)false), vOpt, vOpt);
    					
  4. Compile and run the project.
  5. When the dialog box appears, click Run. The count results are displayed in the Debug window.

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

REFERENCES

For more information, see the following Microsoft Developer Network (MSDN) Web sites: For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

253235 FILE: OFFAUTMN.EXE Discusses Office 97 and 2000 Automation and Provides Sample Code

220607 How To Automate Microsoft Word to Perform Mail Merge from Visual Basic

220911 How To Automate Microsoft Word to Perform a Mail Merge Using Visual C++ and MFC

183369 How To Use Automation to Run a Word Macro with Arguments


Modification Type:MajorLast Reviewed:5/5/2006
Keywords:kbAutomation kbhowto KB293861 kbAudDeveloper