例如:
// 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