Click here to Skip to main content
Licence 
First Posted 29 Apr 2003
Views 110,244
Bookmarked 45 times

Count pages in MS Word Document

By Shelly Jain | 29 Apr 2003
A simple way of using C# to access a Word document's properties
6 votes, 31.6%
1
1 vote, 5.3%
2
2 votes, 10.5%
3
4 votes, 21.1%
4
6 votes, 31.6%
5
3.44/5 - 19 votes
μ 3.20, σa 2.99 [?]

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

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

About the Author

Shelly Jain



United States United States

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionthat assembly does not allow partially trusted callers PinmemberMember 45293164:21 16 Jul '10  
GeneralSome Example Application PinmemberEpstone7:24 16 Feb '10  
QuestionComplete document statistics, including image.. Possible? Pinmemberkartika916:15 26 Aug '09  
GeneralMS Office app is required in the targate machice PinmemberMd. Marufuzzaman21:32 26 Jun '09  
Hi..
To implemtnts and deploy it as COM+ app, is the ms office need to install in the targete machine.
 
Md. Marufuzzaman

GeneralRe: MS Office app is required in the targate machice PinmemberPRMan!!!13:13 9 Dec '09  
GeneralRe: MS Office app is required in the targate machice PingroupMd. Marufuzzaman2:15 11 Dec '09  
GeneralClever PinmemberDerek Bartram9:00 26 Apr '08  
QuestionHow can I access each page of word document PinmemberKKGaurav2:45 17 Sep '07  
GeneralWriting Data to Word Document Pinmembersrujana_m3:13 24 Jul '07  
QuestionPerformance issue PinmemberLokanatha Reddy2:09 3 Apr '07  
QuestionGet Header style PinmemberLokanatha Reddy3:38 27 Mar '07  
GeneralRead content From Multiple word document Files PinmemberSatish_CHVS3:24 1 Sep '06  
QuestionReading MSG-files with C#? PinmemberWolfram Menzel4:13 9 Mar '05  
GeneralNeed save a Word document PinsussAnonymous8:11 1 Sep '04  
QuestionHow to count the number of words in the Document Pinmemberganekun22:35 30 Jul '04  
AnswerRe: How to count the number of words in the Document Pinmembergeorgi nastev7:01 23 Oct '06  
GeneralWord Doc tags Pinmemberkanch eramudu8:56 18 Aug '03  
GeneralOffice XP PinmemberRay Cassick13:08 30 Apr '03  
GeneralRe: Office XP Pinmemberyuronline7:48 26 Aug '07  
QuestionMissing things? PinmemberRay Cassick10:46 30 Apr '03  
AnswerRe: Missing things? PinmemberMisterG5:53 1 Jul '04  

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.

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