C#从Word文档中读取

时间:2016-09-12 12:19:34

标签: ms-word

我试图从Word文档中读取内容,我希望计算机告诉我文档中写的内容是不是要在其他地方写自己。所以,当我说关键字" word"我的程序应该打开一个对话框菜单,让我选择一个word文件并告诉我里面是什么。其他关键字有效。所以,这是我的代码,也是我的错误。

case "word":
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
  Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
  object readFromPath = null;
  Document doc = app.Documents.Open(ref readFromPath);

  foreach (Paragraph objParagraph in doc.Paragraphs)
    ss.SpeakAsync(objParagraph.Range.Text.Trim());

  ((_Document)doc).Close();
  ((_Application)app).Quit();
}

我的错误是enter image description here

1 个答案:

答案 0 :(得分:1)

Application.Documents.Open采用完整路径和文件名。

路径必须以\结尾,并在字符串前加上@(或省略@并加倍反斜杠\,因为一个反斜杠被认为是转义字符)

 object readFromPath = @"C:\Users\N.Horatiu\Desktop\s.docx"

 Document doc = app.Documents.Open(ref readFromPath);