Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用word互操作提取word内容会导致错误_C#_Ms Office_Office Interop - Fatal编程技术网

C# 使用word互操作提取word内容会导致错误

C# 使用word互操作提取word内容会导致错误,c#,ms-office,office-interop,C#,Ms Office,Office Interop,我正在尝试使用以下代码提取word内容: // Open a doc file. Application application = new Application(); Document document = application.Documents.Open("d:\\a.doc"); // Loop through all words in the document. int count =

我正在尝试使用以下代码提取word内容:

// Open a doc file.
            Application application = new Application();
            Document document = application.Documents.Open("d:\\a.doc");

            // Loop through all words in the document.
            int count = document.Words.Count;
            for (int i = 1; i <= count; i++)
            {
                // Write the word.
                string text = document.Words[i].Text;
                Console.WriteLine("Word {0} = {1}", i, text);
            }
            // Close word.
            application.Quit();

我已经安装了office 2013

我终于下载了aspire.docnuget,并使用它提取word文件的内容,如您所见:

   Document document = new Document();
            document.LoadFromFile(@"d:\a.docx");

            //Initialzie StringBuilder Instance
            StringBuilder sb = new StringBuilder();

            //Extract Text from Word and Save to StringBuilder Instance
            foreach (Section section in document.Sections)
            {
                foreach (Paragraph paragraph in section.Paragraphs)
                {
                    sb.AppendLine(paragraph.Text);
                }
            }

            //Create a New TXT File to Save Extracted Text
            Console.WriteLine(sb.ToString());
            Console.ReadLine();

但是在运行之后,我在哪一行得到这个错误?@MattBurland Document=application.Documents.Open(“d:\\a.doc”);尝试
var application=新单词。_application()
var document=application.Documents.Open(@“D:\a.doc”)也许我搞错了,试试
var-application=new-Word.application()。我不记得您是否需要下划线。@Equalsk无法解析Word.application!!!
   Document document = new Document();
            document.LoadFromFile(@"d:\a.docx");

            //Initialzie StringBuilder Instance
            StringBuilder sb = new StringBuilder();

            //Extract Text from Word and Save to StringBuilder Instance
            foreach (Section section in document.Sections)
            {
                foreach (Paragraph paragraph in section.Paragraphs)
                {
                    sb.AppendLine(paragraph.Text);
                }
            }

            //Create a New TXT File to Save Extracted Text
            Console.WriteLine(sb.ToString());
            Console.ReadLine();