Click here to Skip to main content
11,932,048 members (58,751 online)
Click here to Skip to main content
Add your own
alternative version


1 download
48 bookmarked

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

You may also be interested in...

Comments and Discussions

QuestionComputeStatistics Pin
Member 1154151414-Jul-15 13:11
memberMember 1154151414-Jul-15 13:11 
Questionthat assembly does not allow partially trusted callers Pin
Member 452931616-Jul-10 4:21
memberMember 452931616-Jul-10 4:21 
GeneralSome Example Application Pin
Epstone16-Feb-10 7:24
memberEpstone16-Feb-10 7:24 
QuestionComplete document statistics, including image.. Possible? Pin
kartika926-Aug-09 16:15
memberkartika926-Aug-09 16:15 
GeneralMS Office app is required in the targate machice Pin
Md. Marufuzzaman26-Jun-09 21:32
memberMd. Marufuzzaman26-Jun-09 21:32 
GeneralRe: MS Office app is required in the targate machice Pin
PRMan!!!9-Dec-09 13:13
memberPRMan!!!9-Dec-09 13:13 
If you trust the summary information to be correct (it is updated each time Word saves the document using the editor's printer settings, but maybe not for OpenOffice, Abiword, etc.), you could just open the file and do a Regex search for "nofpages(\d+)":

int pages = -1;
string word = File.ReadAllText(pathtodoc);
Match m = Regex.Match(@"noofpages(\d+)", word);
if (m.Success)
  int.TryParse(m.Groups[1].Value, out pages);

Of course, this also only works for documents that aren't huge, but most Word files are relatively small (10s of KB). Even the big ones are usually in the 10s of MB, which is nothing on 1-4GB RAM machines.

This also works for RTF files, BTW.
GeneralRe: MS Office app is required in the targate machice Pin
Md. Marufuzzaman11-Dec-09 2:15
groupMd. Marufuzzaman11-Dec-09 2:15 
GeneralClever Pin
Derek Bartram26-Apr-08 9:00
memberDerek Bartram26-Apr-08 9:00 
QuestionHow can I access each page of word document Pin
KKGaurav17-Sep-07 2:45
memberKKGaurav17-Sep-07 2:45 
AnswerRe: How can I access each page of word document Pin
Sumit Kumar Singh India18-Jul-14 4:54
memberSumit Kumar Singh India18-Jul-14 4:54 
GeneralWriting Data to Word Document Pin
srujana_m24-Jul-07 3:13
membersrujana_m24-Jul-07 3:13 
QuestionPerformance issue Pin
Lokanatha Reddy3-Apr-07 2:09
memberLokanatha Reddy3-Apr-07 2:09 
QuestionGet Header style Pin
Lokanatha Reddy27-Mar-07 3:38
memberLokanatha Reddy27-Mar-07 3:38 
GeneralRead content From Multiple word document Files Pin
Satish_CHVS1-Sep-06 3:24
memberSatish_CHVS1-Sep-06 3:24 
QuestionReading MSG-files with C#? Pin
Wolfram Menzel9-Mar-05 4:13
memberWolfram Menzel9-Mar-05 4:13 
GeneralNeed save a Word document Pin
Anonymous1-Sep-04 8:11
sussAnonymous1-Sep-04 8:11 
QuestionHow to count the number of words in the Document Pin
ganekun30-Jul-04 22:35
memberganekun30-Jul-04 22:35 
AnswerRe: How to count the number of words in the Document Pin
georgi nastev23-Oct-06 7:01
membergeorgi nastev23-Oct-06 7:01 
GeneralWord Doc tags Pin
kanch eramudu18-Aug-03 8:56
memberkanch eramudu18-Aug-03 8:56 
GeneralOffice XP Pin
Ray Cassick30-Apr-03 13:08
memberRay Cassick30-Apr-03 13:08 
GeneralRe: Office XP Pin
yuronline26-Aug-07 7:48
memberyuronline26-Aug-07 7:48 
QuestionMissing things? Pin
Ray Cassick30-Apr-03 10:46
memberRay Cassick30-Apr-03 10:46 
AnswerRe: Missing things? Pin
MisterG1-Jul-04 5:53
memberMisterG1-Jul-04 5:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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
Web01 | 2.8.151126.1 | Last Updated 30 Apr 2003
Article Copyright 2003 by Shelly Jain
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid