Thursday, December 4, 2008

C#: Calculate Statistics of a Word Document

//***
//Label names are self Explanatory
//Stats include content from FootNotes and EndNotes
//Use objFalse = false instead of objMissing to exclude FootNotes and EndNotes
//***

Word.Document WDocument = Globals.ThisAddIn.Application.ActiveDocument;
object objMissing = System.Reflection.Missing.Value;

lblNumberOfPages.Text = WDocument.ComputeStatistics(Word.WdStatistic.wdStatisticPages, ref objMissing).ToString();

lblNumberOfParagraphs.Text = WDocument.ComputeStatistics(Word.WdStatistic.wd
StatisticParagraphs, ref objMissing).ToString();

lblNumberOfSentences.Text = WDocument.Sentences.Count.ToString();

lblNumberOfLines.Text = WDocument.ComputeStatistics(Word.WdStatistic.wdStatisticLines, ref objMissing).ToString();

lblNumberOfWords.Text = WDocument.ComputeStatistics(Word.WdStatistic.wdStatisticWords, ref objMissing).ToString();

lblNumberOfCharactersIncludingSpaces.Text = WDocument.ComputeStatistics(Word.WdStatistic.wdStatisticCharactersWithSpaces, ref objMissing).ToString();

lblNumberOfCharactersExcludingSpaces.Text = WDocument.ComputeStatistics(Word.WdStatistic.wdStatisticCharacters, ref objMissing).ToString();