How to get the page and word count in Microsoft Word



  • Note: be sure to reference the Microsoft.Office.Interop.Word.dll in your project first.
     

    To get the page and word count in Microsoft Word is very simple. First we need to instance the Application and the Document object to open the Word file:
     

    object objMissing = System.Reflection.Missing.Value;

    Microsoft.Office.Interop.Word.Application objWord = new Microsoft.Office.Interop.Word.Application();

    Microsoft.Office.Interop.Word.Document objDoc;

    objWord = new Microsoft.Office.Interop.Word.Application();

    object fileName = "Full path of the File";

    objDoc = objWord.Documents.Open(ref fileName,

    ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,

    ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing,

    ref objMissing, ref objMissing, ref objMissing, ref objMissing, ref objMissing);

    objDoc.Activate();
     

    Then we need to use the WdStatistic class. In there we have an Enum which need to use only two in this case: wdStatisticPages and wdStatisticWords.

    To take the page count do the following:
     

    Microsoft.Office.Interop.Word.WdStatistic stat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticPages;

    int totalPages = objDoc.ComputeStatistics(stat, ref objMissing);
     

    And to take the word count do the following:
     

    stat = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticWords;

    int totalWords = objDoc.ComputeStatistics(stat, ref objMissing);


    Enjoy it!



  • Comments



Add a Comment