Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Count total number of word file pages using Microsoft Office APIs in C# .NET 2.0.

Author: Zunnair
Download Source Code : 715_WordPageCount.zip

In this simple article you will learn that how you can count total number of pages in word file using MS Office and word APIs.

Simply just create New Project of windows application in C#.

 

Import some APIs

 

Interop.Microsoft.Office.Core

Interop.Word

Interop.VBIDE

 

Drag one label, two buttons, one fileOpenDialog object and one textbox on the form.

 

Now write following code on form Load event

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DevAsp Word Page Counter";

            button1.Text = "Browse";

            button2.Text = "Count Pages";

        }

 

Now write code on buttons events

private void button2_Click(object sender, EventArgs e)

        {

            MessageBox.Show(GetNoOfPagesDOC(textBox1.Text).ToString());

        }

 

private void button1_Click(object sender, EventArgs e)

        {

            if (openFileDialog1.ShowDialog() == DialogResult.OK)

            {

                textBox1.Text = openFileDialog1.FileName;

            }

        }

 

Now this is the function which count pages of word

public int GetNoOfPagesDOC(string FileName)

        {

            int num = 0;

            object fileName = FileName;

            object readOnly = false;

            object isVisible = true;

            object objDNS = Word.WdSaveOptions.wdDoNotSaveChanges;

            try

            {

                Word.ApplicationClass WordApp = new Word.ApplicationClass();

 

                // give any file name of your choice.

 

 

                //  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,

                                         ref missing, ref missing, ref missing, ref missing);

                Word.WdStatistic stat = Word.WdStatistic.wdStatisticPages;

                num = aDoc.ComputeStatistics(stat, ref missing);

                WordApp.Quit(ref objDNS, ref missing, ref missing);

                aDoc = null;

                WordApp = null;

                GC.Collect();

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

            return num;

 

        }

 

In this funciton we are opening word file with WordApp.Documents.Open(..) function and then using aDoc.ComputeStatistics(…) function we count total pages of word file.

 

This is the simple page counting source.

 

Article Comments
Thank you it was really of help...

Posted on 8/18/2008 6:34:57 PM by Avinash

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Working with Checked and UnChecked Keywords in .Net 2.0.

Disclaimer - Privacy
© 2002-2017 DevASP.net