Some time ago, I was working on a Plagiarism detection system for my school. When I read a large number of Word documents, the request for a cut operation often failed, for example:
The source code used for reading Word documents is as follows:
Protected void openfile (RichTextBox RTB, string filenametemp) {try {Microsoft. office. interOP. word. application mywordapp = new Microsoft. office. interOP. word. applicationclass (); object file = filenametemp; object nullobj = system. reflection. missing. value; object readonly = false; object isvisible = true; Microsoft. office. interOP. word. document Doc = mywordapp. documents. open (ref file, ref nullobj, ref readonly, ref nullobj, ref nullobj, ref nullobj, ref isvisible, ref nullobj, ref nullobj); Doc. activewindow. selection. wholestory (); Doc. activewindow. selection. copy (); idataobject DATA = clipboard. getdataobject (); RTB. TEXT = data. getdata (dataformats. text ). tostring (); Doc. close (ref nullobj, ref nullobj, ref nullobj); // close the wordapp Component Object mywordapp. quit (ref nullobj, ref nullobj, ref nullobj);} catch (exception ex) {MessageBox. show (ex. message );}}
Press Ctrl + V to post the document you just read. I thought it was because the clipboard was not cleared, so I added clipboard. Clear (); Code:
RTB. Text = data. getdata (dataformats. Text). tostring ();
Clipboard. Clear ();
Doc. Close (ref nullobj, ref nullobj, ref nullobj); // close the wordapp Component Object mywordapp. Quit (ref nullobj, ref nullobj, ref nullobj );}
However, a new problem occurs again. First, the following pop-up occurs:
The following dialog is displayed:
An evil clipboard !! Finally, search around the Internet and finally find that you can read the Word documents without a clipboard, and there are no similar problems after testing.
The improved code is as follows:
Protected void openfile (RichTextBox RTB, string filenametemp) {try {Microsoft. office. interOP. word. application mywordapp = new Microsoft. office. interOP. word. applicationclass (); object file = filenametemp; object nullobj = system. reflection. missing. value; object readonly = false; object isvisible = true; Microsoft. office. interOP. word. document Doc = mywordapp. documents. open (ref file, ref nullobj, ref readonly, ref nullobj, ref nullobj, ref nullobj, ref isvisible, ref nullobj, ref nullobj); // Doc. activewindow. selection. wholestory (); // Doc. activewindow. selection. copy (); Doc. save (); // idataobject DATA = clipboard. getdataobject (); // RTB. TEXT = data. getdata (dataformats. text ). tostring (); RTB. TEXT = Doc. content. text; Doc. close (ref nullobj, ref nullobj, ref nullobj); // close the wordapp Component Object // mywordapp = NULL; clipboard. clear (); mywordapp. quit (ref nullobj, ref nullobj, ref nullobj);} catch (exception ex) {MessageBox. show (ex. message );}}
Welcome to reprint, if you have reprinted please indicate the article from: http://blog.csdn.net/q345852047