![]() |
Languages »
C# »
COM Interop
Intermediate
Count pages in MS Word DocumentBy Shelly JainA simple way of using C# to access a Word document's properties |
C#, Windows, .NET 1.0, Visual Studio, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||
This article explains how to count the pages in MS Word document programmatically using C#.
To count the pages in MSWord document, we need to follow following steps:
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();
}
}
}
General
News
Question
Answer
Joke
Rant
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 |