例如:
// open the file
Word.ApplicationClass app = new Word.ApplicationClass();
object path = @"c:\Users\name\Desktop\Весь набор.docx";
object missing = System.Reflection.Missing.Value;
Word.Document doc = null;
try
{
doc = app.Documents.Open(ref path,
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);
// index
foreach (Word.Section section in doc.Sections)
{
Debug.WriteLine("Section index:" + section.Index);
Debug.WriteLine("section start: " + section.Range.Start + ", section end: " + section.Range.End);
}
bool processNextTable = false;
foreach (Word.Paragraph paragraph in doc.Paragraphs)
{
string toWrite = paragraph.Range.Text;
System.Diagnostics.Debug.WriteLine(toWrite);
}
foreach (Word.Table table in doc.Tables)
{
foreach (Word.Row wRow in table.Rows)
foreach (Word.Cell cell in wRow.Cells)
{
}
}
}
finally
{
if (doc != null)
{
bool saveChanges = false; // temporary not save any changes
app.Quit(ref saveChanges, ref missing, ref missing);
}
}
感谢很多解释。 所以基本上我们不能枚举文档元素。我问这个问题是因为我在处理单词表和不同形状时遇到问题。有时候,word API会从文档中跳过一些单词,从而导致我的程序失败。此外,API会给出错误的句子,如果句子是“作为高级程序员”,那么我会得到“工作为高级”。作为一个句子,'程序员'作为第二个。它应该是单个句子。我想避免这些问题。另外还有更多的interop API问题。 – Shekhar 2010-08-18 12:45:02
@shekhar:当然你可以遍历内容,但不能以简单的方式。 Word可让您完全访问所有对象。关于句子分割,你需要考虑到这在自然语言处理中并不是一个微不足道的研究课题。 – 2010-08-18 12:49:57
是否可以迭代内容?怎么做? – Shekhar 2010-08-18 12:52:35