.net 讀取 word 文字

in Visual Studio .net IDE

?

Add references: in COM tab, find out "Microsoft Word blahblah" component.

Example code:

OpenFileDialog openFile = new OpenFileDialog();
openFile.InitialDirectory = System.AppDomain.CurrentDomain.BaseDirectory;

if(openFile.ShowDialog() == DialogResult.Cancel)
{
??? return;
}

object fileName = (object)openFile.FileName;

Word.Application x = new Word.ApplicationClass();
Word.Document d = new Word.DocumentClass();

Type wordType = x.GetType();
Type docType = d.GetType();

object nothing = System.Reflection.Missing.Value;

d = x.Documents.OpenOld(ref fileName, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing);

string str = "";

foreach (Word.Range r in x.ActiveDocument.Words)
{
??? str = str + " " + r.Text;
}

MessageBox.Show(str);

if (d != null)
{
??? d.Close(
??????? /* ref object SaveChanges */ ref nothing,
??????? /* ref object OriginalFormat */ ref nothing,
??????? /* ref object RouteDocument */ ref nothing);
??? d = null;
}

if (x != null)
{
??? x.Quit(
??????? /* ref object SaveChanges */ ref nothing,
??????? /* ref object OriginalFormat */ ref nothing,
??????? /* ref object RouteDocument */ ref nothing);
??? x = null;
}