Skip to main content
Email Password   helpLost your password?

Introduction

This article explains how to count the pages in MS Word document programmatically using C#.

Using the code

To count the pages in MSWord document, we need to follow following steps:

  1. Create an object of Word Application.
  2. Open the required Word document using the open() function of Documents collection of Word document. This function returns the Word.Document object.
  3. Cal the ComputeStatistics fumction on Word.Document object passing it an enum WdStatistic with value equal to wdStatisticPages. This function will return the number of pages in the opened word document.

Here is the Code:

using System; 

namespace DocPageCounter 
{ 
      class PageCounter 
      { 
            /// <SUMMARY> 

            /// The main entry point for the application. 

            /// </SUMMARY> 

            [STAThread] 
            static void Main(string[] args) 
            { 
                Word.ApplicationClass WordApp = new Word.ApplicationClass(); 

                // give any file name of your choice. 

                object fileName = "D:\\abc\\oop1.doc"; 
                object readOnly = false; 
                object isVisible = true;

                //  the way to handle parameters you don't care about in .NET 

                object missing = System.Reflection.Missing.Value; 

                //   Make word visible, so you can see what's happening 

                //WordApp.Visible = true; 

                //   Open the document that was chosen by the dialog 

                Word.Document aDoc = WordApp.Documents.Open(ref fileName, 
                                        ref missing,ref readOnly, ref missing,
                                        ref missing, ref missing, ref missing,
                                        ref missing, ref missing, ref missing,
                                         ref missing, ref isVisible);

                Word.WdStatistic stat = Word.WdStatistic.wdStatisticPages ; 
                int num =  aDoc.ComputeStatistics(stat,ref missing); 
                System.Console.WriteLine ("The number of pages in doc is {0}", 
                                          num); 
                System.Console.ReadLine(); 
            } 
      } 
} 
You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralComplete document statistics, including image.. Possible? Pin
kartika9
16:15 26 Aug '09  
GeneralMS Office app is required in the targate machice Pin
Md. Marufuzzaman
21:32 26 Jun '09  
GeneralClever Pin
Derek Bartram
9:00 26 Apr '08  
GeneralHow can I access each page of word document Pin
KKGaurav
2:45 17 Sep '07  
GeneralWriting Data to Word Document Pin
srujana_m
3:13 24 Jul '07  
QuestionPerformance issue Pin
Lokanatha Reddy
2:09 3 Apr '07  
QuestionGet Header style Pin
Lokanatha Reddy
3:38 27 Mar '07  
GeneralRead content From Multiple word document Files Pin
Satish_CHVS
3:24 1 Sep '06  
GeneralReading MSG-files with C#? Pin
Wolfram Menzel
4:13 9 Mar '05  
GeneralNeed save a Word document Pin
Anonymous
8:11 1 Sep '04  
GeneralHow to count the number of words in the Document Pin
ganekun
22:35 30 Jul '04  
GeneralRe: How to count the number of words in the Document Pin
georgi nastev
7:01 23 Oct '06  
GeneralWord Doc tags Pin
kanch eramudu
8:56 18 Aug '03  
GeneralOffice XP Pin
Ray Cassick
13:08 30 Apr '03  
GeneralRe: Office XP Pin
yuronline
7:48 26 Aug '07  
GeneralMissing things? Pin
Ray Cassick
10:46 30 Apr '03  
GeneralRe: Missing things? Pin
MisterG
5:53 1 Jul '04  


Last Updated 29 Apr 2003 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009