Click here to Skip to main content
15,880,796 members
Articles / Programming Languages / C#
Article

Count pages in MS Word Document

Rate me:
Please Sign up or sign in to vote.
3.44/5 (19 votes)
29 Apr 2003 172.6K   48   23
A simple way of using C# to access a Word document's properties

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:

C#
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(); 
            } 
      } 
} 

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionComputeStatistics Pin
Member 1154151414-Jul-15 12:11
Member 1154151414-Jul-15 12:11 
Questionthat assembly does not allow partially trusted callers Pin
Member 452931616-Jul-10 3:21
Member 452931616-Jul-10 3:21 
GeneralSome Example Application Pin
Epstone16-Feb-10 6:24
Epstone16-Feb-10 6:24 
QuestionComplete document statistics, including image.. Possible? Pin
kartika926-Aug-09 15:15
kartika926-Aug-09 15:15 
GeneralMS Office app is required in the targate machice Pin
Md. Marufuzzaman26-Jun-09 20:32
professionalMd. Marufuzzaman26-Jun-09 20:32 
GeneralRe: MS Office app is required in the targate machice Pin
PRMan!!!9-Dec-09 12:13
PRMan!!!9-Dec-09 12:13 
GeneralRe: MS Office app is required in the targate machice Pin
Md. Marufuzzaman11-Dec-09 1:15
professionalMd. Marufuzzaman11-Dec-09 1:15 
GeneralClever Pin
Derek Bartram26-Apr-08 8:00
Derek Bartram26-Apr-08 8:00 
QuestionHow can I access each page of word document Pin
KKGaurav17-Sep-07 1:45
KKGaurav17-Sep-07 1:45 
AnswerRe: How can I access each page of word document Pin
Sumit Kumar Singh India18-Jul-14 3:54
Sumit Kumar Singh India18-Jul-14 3:54 
GeneralWriting Data to Word Document Pin
srujana_m24-Jul-07 2:13
srujana_m24-Jul-07 2:13 
QuestionPerformance issue Pin
Lokanatha Reddy3-Apr-07 1:09
Lokanatha Reddy3-Apr-07 1:09 
QuestionGet Header style Pin
Lokanatha Reddy27-Mar-07 2:38
Lokanatha Reddy27-Mar-07 2:38 
GeneralRead content From Multiple word document Files Pin
Satish_CHVS1-Sep-06 2:24
Satish_CHVS1-Sep-06 2:24 
QuestionReading MSG-files with C#? Pin
Wolfram Menzel9-Mar-05 3:13
Wolfram Menzel9-Mar-05 3:13 
GeneralNeed save a Word document Pin
Anonymous1-Sep-04 7:11
Anonymous1-Sep-04 7:11 
QuestionHow to count the number of words in the Document Pin
ggmemo30-Jul-04 21:35
ggmemo30-Jul-04 21:35 
AnswerRe: How to count the number of words in the Document Pin
georgi nastev23-Oct-06 6:01
georgi nastev23-Oct-06 6:01 
GeneralWord Doc tags Pin
kanch eramudu18-Aug-03 7:56
kanch eramudu18-Aug-03 7:56 
GeneralOffice XP Pin
Ray Cassick30-Apr-03 12:08
Ray Cassick30-Apr-03 12:08 
GeneralRe: Office XP Pin
yuronline26-Aug-07 6:48
yuronline26-Aug-07 6:48 
QuestionMissing things? Pin
Ray Cassick30-Apr-03 9:46
Ray Cassick30-Apr-03 9:46 
AnswerRe: Missing things? Pin
MisterG1-Jul-04 4:53
MisterG1-Jul-04 4:53 
Try this :

Word.Document aDoc = WordApp.Documents.Open2000(ref fileName, <br />
				ref missing,ref readOnly, ref missing,<br />
				ref missing, ref missing, ref missing,<br />
				ref missing, ref missing, ref missing,<br />
				ref missing, ref isVisible);

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.