我编写了一些代码,根据以下网址中的建议解析Microsoft Word文档中的句子:
Using VBA to parse text in an MS Word document
How to Automate Microsoft Word using C#
我写了一个小函数,它读入文档并通过调试语句输出它的句子:
using Microsoft.Office.Interop.Word;
private void button2_Click(object sender, EventArgs e)
{
oWord.Visible = true;
object filename = textBox1.Text;
oDoc = oWord.Documents.Open(filename, ref oMissing, true, false, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
Sentences sentences;
sentences = oDoc.Sentences;
Debug.WriteLine("sentences=" + sentences.ToString());
foreach (Range r in sentences)
{
Debug.WriteLine("range.Text=" + r.Text);
}
}
它与我之前在字符串上使用Mid
函数所做的工作一样好。考虑到MS Word具有语法检查功能,我期待它做得更好。有没有办法利用MS Word的语法能力,以使其更聪明地解析句子?