如果想统计中文字符,则必须使用
WdStatistic
示例代码如下:
public static void WordCount(string filePath)
{
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;
Microsoft.Office.Interop.Word.WdStatistic statsAsia = Microsoft.Office.Interop.Word.WdStatistic.wdStatisticFarEastCharacters;
try
{
wordApp = new Microsoft.Office.Interop.Word.Application();
doc = wordApp.Documents.Open(filePath);
//doc.Content.Text = text;
wordCount = doc.ComputeStatistics(stats, ref includeFootnotesAndEndnotes);
int asiaWordsCount = doc.ComputeStatistics(statsAsia, ref includeFootnotesAndEndnotes);
wordApp.Quit(ref saveChanges, ref missing, ref missing);
}
catch (Exception) { }
finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
doc = null;
}
}