// Add a reference to Microsoft Word 11.0 Object Library to the project reference
Private void button#click (Object sender, system. eventargs E)
{
// Call the open file dialog box to obtain the Word file, RTF file, and text file path name of the file to be opened.
Openfiledialog OPD = new openfiledialog ();
OPD. initialdirectory = "C ://";
OPD. filter = "Word Document (*. doc) | *. doc | Text Document (*. TXT) | *. TXT | RTF document (*. RTF) | *. RTF | all documents (*. *) | *. *";
OPD. filterindex = 1;
If (OPD. showdialog () = dialogresult. OK & OPD. filename. length> 0)
{
// Create an instance of the word class. disadvantage: the display of tables, images, and so on cannot be read correctly.
Word. applicationclass APP = new word. applicationclass ();
Word. Document Doc = NULL;
Object missing = system. reflection. Missing. value;
Object filename = OPD. filename;
Object readonly = false;
Object isvisible = true;
Object Index = 0;
Try
{
Doc = app. Documents. Open (ref filename, ref missing, ref readonly,
Ref missing, ref missing,
Ref missing, ref isvisible, ref missing,
Ref missing, ref missing, ref missing );
Doc. activewindow. selection. wholestory ();
Doc. activewindow. selection. Copy ();
// Obtain data from the clipboard
Idataobject DATA = clipboard. getdataobject ();
This. richtextbox1.text = data. getdata (dataformats. Text). tostring ();
}
Finally
{
If (Doc! = NULL)
{
Doc. Close (ref missing, ref missing, ref missing );
Doc = NULL;
}
If (app! = NULL)
{
App. Quit (ref missing, ref missing, ref missing );
APP = NULL;
}
}
}
}