質問

So I am currently trying to convert a word doc (.doc) into a text document because I want to use regular expressions on it to find things in the document. So I came up with the below and it converts the word document into a rich text format (by appending it to a rich text box), but this does not translate into a plain text format. When I tried with regular text document it printed every word on a new line. I have not been able to find any information on how to do this in C#. I'm using C# and visual studio 2010.

I do not expect any special characters in the document (like bold, underlines, etc.), but if someone knows how I can be robust and extract those that would be super awesome.

I want it as a text document because there's several methods I know I can use on regular text, but I doubt they would work on word text due to hidden/special characters that come with word docs.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;

namespace ReadWordDocProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string testFile = @"C:\Users\<mycomputer>\Documents\TestItemHelpers\TestWordDoc.docx";

            Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
            Document document = application.Documents.Open(testFile);//path here

            int count = document.Words.Count;
            for (int i = 1; i <= count; i++)
            {
                string text = document.Words[i].Text;
                //Do output with text here
                richTextBox1.AppendText(text);
            }

            ((_Application)application).Quit(); //cast as _Application because there's ambiguity 
        }


    }
}
役に立ちましたか?

解決

Microsoft says you shouldn't use Microsoft Office Interop to manipulate documents in an automated application.

You can use a free library like Spire Doc to convert a Word Doc to TXT, then open the txt file. I think there is a way to save directly to MemoryStream from Spire, but I'm not sure. (I know there is in Aspose Words, but that isn't free).

private void button1_Click(object sender, EventArgs e)
{
    //Open word document
    Document document = new Document();
    string docPath = @"C:\Users\<computer name>\Documents\TestItemHelpers";

    document.LoadFromFile(Path.Combine(docPath,"TestWordDoc.docx"));

    //Save doc file.
    document.SaveToFile(Path.Combine(docPath,"TestTxt.txt"), FileFormat.Txt);

    string readText = File.ReadAllText(Path.Combine(docPath,"TestTxt.txt"));

    //do regex here
}

Edit: If you're going to use Interop because it is okay for user-run activities (as pointed out in comments), you can save the document as a text file then do the regex:

private void button1_Click(object sender, EventArgs e)
{
    string docPath = @"C:\Users\<computer name>\Documents\TestItemHelpers"
    string testFile = "TestWordDoc.docx";

    Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
    Document document = application.Documents.Open(Path.Combine(docPath,testFile );
    application.ActiveDocument.SaveAs(Path.Combine(docPath,"TestTxt.txt"), WdSaveFormat.wdFormatText, ref noEncodingDialog);
    ((_Application)application).Quit();

    string readText = File.ReadAllText(Path.Combine(docPath,"TestTxt.txt"));

    //do regex here
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top