5,545,925 members and growing! (18,036 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, .NET, Visual Studio, Dev

Posted: 29 Apr 2003
Updated: 29 Apr 2003
Views: 75,047
Bookmarked: 22 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
16 votes for this Article.
Popularity: 3.75 Rating: 3.11 out of 5
6 votes, 37.5%
1
1 vote, 6.3%
2
1 vote, 6.3%
3
3 votes, 18.8%
4
5 votes, 31.3%
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



Location: United States United States

Other popular C# articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralClevermemberDerek Bartram9:00 26 Apr '08  
GeneralHow can I access each page of word documentmemberKKGaurav2:45 17 Sep '07  
GeneralWriting Data to Word Documentmembersrujana_m3:13 24 Jul '07  
QuestionPerformance issuememberLokanatha Reddy2:09 3 Apr '07  
QuestionGet Header stylememberLokanatha Reddy3:38 27 Mar '07  
GeneralRead content From Multiple word document FilesmemberSatish_CHVS3:24 1 Sep '06  
GeneralReading MSG-files with C#?memberWolfram Menzel4:13 9 Mar '05  
GeneralNeed save a Word documentsussAnonymous8:11 1 Sep '04  
GeneralHow to count the number of words in the Documentmemberganekun22:35 30 Jul '04  
GeneralRe: How to count the number of words in the Documentmembergeorgi nastev7:01 23 Oct '06  
GeneralWord Doc tagsmemberkanch eramudu8:56 18 Aug '03  
GeneralOffice XPmemberRay Cassick13:08 30 Apr '03  
GeneralRe: Office XPmemberyuronline7:48 26 Aug '07  
GeneralMissing things?memberRay Cassick10:46 30 Apr '03  
GeneralRe: Missing things?memberMisterG5: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-2008
Web09 | Advertise on the Code Project