C # solution to the failure of the clipboard operation requested to frequently read Word Files

Source: Internet
Author: User

 

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.