Counting words in a Word document

Last post 06-28-2009 2:57 PM by graham123. 2 replies.

Sort Posts:

  • Counting words in a Word document

    06-28-2009, 11:14 AM
    • Member
      2 point Member
    • graham123
    • Member since 11-18-2007, 6:43 AM
    • Posts 4

    Does anybody know how to count accurately the words in an MSWord document?

    I have tried the following, which creates a doc, adds the text, and then attempts to count the words, but it always comes out too high, I think because it counts line feeds and other characters ignored if you were to open the document in Office Word and check the word count there:

    Microsoft.Office.Interop.Word._Application oWord;
    oWord = new Microsoft.Office.Interop.Word.Application();
    oPageDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    oPageDoc.Content.Text = text;
    oPageDoc.Content.Words.Count;

    Any help appreciated!

  • Re: Counting words in a Word document

    06-28-2009, 1:20 PM
    Answer
    • Contributor
      7,249 point Contributor
    • whighfield
    • Member since 01-02-2006, 10:37 PM
    • Winterpeg, Manitoba
    • Posts 1,205

    Using the native Words.Count will always come back with more words that you think because it is scanning the entire document, you need to use the ComputeStatistics method to get what you need.  Also when dealing with COM objects in .NET you should be doing proper cleanup or you will end up with hundreds of WINWORD.EXE processes left running and your computer will run out of resources.

    public static int GetWordCount(string text)
    {
        Microsoft.Office.Interop.Word.Application wordApp = null;
        Microsoft.Office.Interop.Word.Document doc = null;
    
        int wordCount = 0;
        object missing = Type.Missing;
        object saveChanges = false;
        object includeFootnotesAndEndnotes = false;
        Microsoft.Office.Interop.Word.WdStatistic stats = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticWords;
        try
        {
            wordApp = new Microsoft.Office.Interop.Word.Application();
            doc = wordApp.Documents.Add(ref missing, ref missing, ref missing, ref missing);
            doc.Content.Text = text;
            wordCount = doc.ComputeStatistics(stats, ref includeFootnotesAndEndnotes);
            wordApp.Quit(ref saveChanges, ref missing, ref missing);
        }
        catch (Exception) { }
        finally
        {
            System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
            doc = null;
        }
    
        return wordCount;
    }
    Please mark the most helpful reply/replies as "Answer".

    - William
    http://thefrozencoder.ca
  • Re: Counting words in a Word document

    06-28-2009, 2:57 PM
    • Member
      2 point Member
    • graham123
    • Member since 11-18-2007, 6:43 AM
    • Posts 4

    Works a treat. Thanks a lot!

Page 1 of 1 (3 items)