Saturday, January 18, 2014

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

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);
        }  

Reference:
http://stackoverflow.com/questions/5824258/programmatically-count-number-of-characters-words-paragraphs-with-a-specific-sty?rq=1

1 comment:

Image noise comparison methods

 1. using reference image technique     - peak_signal_noise_ratio (PSNR)     - SSI 2. non-reference image technique     - BRISQUE python pac...