|
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:
- Create an object of Word Application.
- Open the required Word document using the open() function of Documents collection of Word document. This function returns the Word.Document object.
- 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
{
[STAThread]
static void Main(string[] args)
{
Word.ApplicationClass WordApp = new Word.ApplicationClass();
object fileName = "D:\\abc\\oop1.doc";
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
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();
}
}
}
| You must Sign In to use this message board. |
|
| | Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh) | FirstPrevNext |
|
 |
|
|
I'm not sure this justified an article personally, however it's kinda clever, I didn't know you could do that!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Maam I want to save each page in Word.Page type object so that I can play only with a single page of a document.My requirement is to save each page of a document as a html page.But still I am only able to whole document as a html document.So please suggest to access one by one page.
Krishan
KK Gaurav App Developer Religare Securities Ltd Delhi
|
| Sign In·View Thread·PermaLink | 3.00/5 (2 votes) |
|
|
|
 |
|
|
Hi, I have some code that generates an xml file and word document parallely. I have to read the xml file and write that data to word document. Writing to word document is based on the xml nodes that is data in first node should go to the first page in the doc file similarly second node text to second page so on. So can anyone help me out
Thnx in advance.
Srujana
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
|
Hi, I tried to read all paragraphs from sample doc. It took 6 mins for 30 pages. Is there any other alternate approach to do the same? What about VBA and macros?
Regards, Loka
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Hi, How to get heading style? I want to read paragraphs and table contents based on heading style.
Loka
|
| Sign In·View Thread·PermaLink | 2.00/5 (2 votes) |
|
|
|
 |
|
|
Hi.. i am selcting multiple number of Word document files. i want read the data from that files,i.e.copy the content from that and edit in a single document without merging.(concatenation) is it possible by using asp.net with C#.
satish
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
Does anybody have an example where IStorage is used in C#? I have to write a program, which has to read data from MSG-files created by Outlook. I would be very glad for any answer to "wolfram.menzel@siemens.com". Thanks a lot.
Wolfram Menzel Siemens AG, Germany I&S GIO IP 2 Tel. +49(0)9131-731897 Handy: 0173 39 275 61
|
| Sign In·View Thread·PermaLink | 2.13/5 (5 votes) |
|
|
|
 |
|
|
hi, i need to save a word document, but i want to force the name of the document and the directory, i don´t want the dialog box from the word for save the file. thank's Andres (Argentina)
|
| Sign In·View Thread·PermaLink | 2.63/5 (8 votes) |
|
|
|
 |
|
|
Hi all.
I am studying on how to access MS Word Documents. I still can't find a way to count the number of words in a document file. Any help or hint I can get?
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|
|
You can count the words in the same way as this counting the pages, but instead of this line: Word.WdStatistic stats = Word.WdStatistic.wdStatisticPages
you can use: Word.WdStatistic stats = Word.WdStatistic.wdStatisticWords
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 I am reading a word document and write it to another Word doc. I need to do some editing to the paragraphs before writing it back. In order to do that, i have to find the style of the word. For example if it is a "heading 1" or "Title". etc. I tried to use the property "style", but it doesn't work. Is there any other method i can use?
Kanch
|
| Sign In·View Thread·PermaLink | 1.57/5 (6 votes) |
|
|
|
 |
|
|
I tested this out with Office XP and, after adding the COM reference to the Word object library, was getting the following error:
No overload for method 'Open' takes '12' arguments After some investigation of the .Open method This is the change I needed to make to the code to get the example to work:
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); I am not sure if this is ONLY for Office XP users because I don't have another version handy to test it on.
Paul Watson wrote: "At the end of the day it is what you produce that counts, not how many doctorates you have on the wall." George Carlin wrote: "Don't sweat the petty things, and don't pet the sweaty things." Jörgen Sigvardsson wrote: If the physicists find a universal theory describing the laws of universe, I'm sure the a**hole constant will be an integral part of that theory.
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
1 - No sample project?
2 - Code will not build as it sits because of a missing reference to the Microsoft Word object library.
3 - Event after I added a ref to the Word object libray I get an error:
No overload for method 'Open' takes '12' arguments
I have not looked into it too closely (heading into a meeting right now) but....
Paul Watson wrote: "At the end of the day it is what you produce that counts, not how many doctorates you have on the wall." George Carlin wrote: "Don't sweat the petty things, and don't pet the sweaty things." Jörgen Sigvardsson wrote: If the physicists find a universal theory describing the laws of universe, I'm sure the a**hole constant will be an integral part of that theory.
|
| Sign In·View Thread·PermaLink | 2.00/5 (3 votes) |
|
|
|
 |
|
|
Try this :
Word.Document aDoc = WordApp.Documents.Open2000(ref fileName, ref missing,ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible);
|
| Sign In·View Thread·PermaLink | 2.00/5 (4 votes) |
|
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|