Click here to Skip to main content
6,596,602 members and growing! (22,115 online)
Email Password   helpLost your password?
Languages » C# » COM Interop     Intermediate

Count pages in MS Word Document

By Shelly Jain

A simple way of using C# to access a Word document's properties
C#, Windows, .NET 1.0, Visual Studio, Dev
Posted:29 Apr 2003
Views:85,190
Bookmarked:33 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
17 votes for this article.
Popularity: 3.82 Rating: 3.11 out of 5
6 votes, 35.3%
1
1 vote, 5.9%
2
2 votes, 11.8%
3
3 votes, 17.6%
4
5 votes, 29.4%
5

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


Member

Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 17 of 17 (Total in Forum: 17) (Refresh)FirstPrevNext
GeneralComplete 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  
GeneralClever PinmemberDerek Bartram9:00 26 Apr '08  
GeneralHow 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  
GeneralReading MSG-files with C#? PinmemberWolfram Menzel4:13 9 Mar '05  
GeneralNeed save a Word document PinsussAnonymous8:11 1 Sep '04  
GeneralHow to count the number of words in the Document Pinmemberganekun22:35 30 Jul '04  
GeneralRe: 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  
GeneralMissing things? PinmemberRay Cassick10:46 30 Apr '03  
GeneralRe: Missing things? PinmemberMisterG5:53 1 Jul '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 29 Apr 2003
Editor: Chris Maunder
Copyright 2003 by Shelly Jain
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project