Click here to Skip to main content
Click here to Skip to main content

Count pages in MS Word Document

, 29 Apr 2003
Rate this:
Please Sign up or sign in to vote.
A simple way of using C# to access a Word document's properties


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> 
            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}", 


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Shelly Jain

United States United States
No Biography provided

Comments and Discussions

Questionthat assembly does not allow partially trusted callers PinmemberMember 452931616-Jul-10 4:21 
GeneralSome Example Application PinmemberEpstone16-Feb-10 7:24 
QuestionComplete document statistics, including image.. Possible? Pinmemberkartika926-Aug-09 16:15 
GeneralMS Office app is required in the targate machice PinmemberMd. Marufuzzaman26-Jun-09 21:32 
GeneralRe: MS Office app is required in the targate machice PinmemberPRMan!!!9-Dec-09 13:13 
GeneralRe: MS Office app is required in the targate machice PingroupMd. Marufuzzaman11-Dec-09 2:15 
GeneralClever PinmemberDerek Bartram26-Apr-08 9:00 
QuestionHow can I access each page of word document PinmemberKKGaurav17-Sep-07 2:45 
AnswerRe: How can I access each page of word document PinmemberSumit Kumar Singh India18-Jul-14 4:54 
GeneralWriting Data to Word Document Pinmembersrujana_m24-Jul-07 3:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 30 Apr 2003
Article Copyright 2003 by Shelly Jain
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid