我需要以编程方式计算已在DOCX文档中应用特定已知样式的字符和/或单词和/或段落。
我需要知道1)如果这是可能的,并且2)任何关于我可以开始解决这个问题的提示。
我熟悉DOM导航,XPath / XQuery,并且可以使用.Net,PHP或Java或任何其他工具,只要我能解决这个问题。
答案 0 :(得分:2)
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);
}