Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically count number of characters/words/paragraphs with a specific style in a DOCX document

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.

like image 752
andrerav Avatar asked Apr 28 '11 20:04

andrerav


People also ask

How do you count words in a specific paragraph in word?

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.

How do you do a word count on DOCX?

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.

Can you count the number of characters in a word document?

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.


1 Answers

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);
        }  
like image 50
manish Avatar answered Oct 20 '22 17:10

manish