I need to programmatically count the characters and/or words and/or paragraphs which have been applied a specific known style in a DOCX document.
I need to know 1) if this is possible and to 2) any hints as to where I can start to get going to solve this problem.
I am familiar with DOM navigation, XPath/XQuery, and can use .Net, PHP or Java or any other tool as long as I can solve this problem.
To count the number of words in only part of your document, select the text you want to count. Then on the Tools menu, click Word Count. Just like the Word desktop program, Word for the web counts words while you type.
Click on the word count in the status bar to see the number of characters, lines, and paragraphs in your document. Click in your document where you want the word count to appear. Click Insert > Quick Parts > Field. In the Field nameslist, click NumWords, and then click OK.
You can get a character count in a Word document by selecting the "Review" tab and clicking "Word Count." You can find both the number of characters with spaces and the character count not including spaces. You can add the Word Count dialog box to the Quick Access toolbar so it's always one click away.
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
try
{
object fileName = @"C:\TT\change.docx";
doc = word.Documents.Open(ref fileName,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
doc.Activate();
int count = doc.Characters.Count ;
int words = doc.Words.Count; ;
int paragraphs = doc.Paragraphs.Count;
doc.Save();
doc.Close(ref missing, ref missing, ref missing);
word.Application.Quit(ref missing, ref missing, ref missing);
}
catch (Exception ex)
{
doc.Close(ref missing, ref missing, ref missing);
word.Application.Quit(ref missing, ref missing, ref missing);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With