Reputation: 393
I need to programmatically count the characters and/or words and/or paragraphs which have been applied a specific known style in a DOCX document.
I need to know 1) if this is possible and to 2) any hints as to where I can start to get going to solve this problem.
I am familiar with DOM navigation, XPath/XQuery, and can use .Net, PHP or Java or any other tool as long as I can solve this problem.
Upvotes: 6
Views: 4460
Reputation: 312
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);
}
Upvotes: 2