C # When the word is disabled, the system prompts "this file is being used by another application or user" normal. Dot

Source: Internet
Author: User
The project needs to convert a DOC file into an HTML file and display it on the web page. at the beginning of the project, no problems were found after the demo was done. When the project started, the problem came! First, after being converted to an HTML file, when opening the HTML file, the system sometimes prompts that the file is being used or something like that. The powerful Google results show that, because the memory is not released in time (the running speed is incorrect, because I quit the word object), I will let the thread stay in the future for half a second. sleep (500);, and the problem is fixed. everything is normal. second: The second problem occurs again after it is generated. That is, when you open a local word Program (even different files) and then convert it to HTML using C, when the HTML is opened, a prompt such as "in use" is reported again. at this time, I felt strange that I did not open the files opened on the page locally. finally, I found that I only exited and did not close the code: Object unknown = type. missing; docs. close (ref unknown, ref unknown, ref unknown); exit code: wordtype. invokemember ("quit", system. reflectio N. bindingflags. invokemethod, null, appclass, null); // after exiting and closing, barely implement it, before the page is opened (that is, when you exit word after converting to HTML), the system prompts "this file is being used by another application or user" C: \ Documents ents and settings .. \ normal. dot actually means that you have updated the template. Do you want to replace it! In this case, open a word and select "tool-> template-> remove the check box for auto update styles" (this is what I said online, but I tried it, in this way, the company's computer can solve the problem, but my notebook cannot solve it). Finally, in the applicationclass object, there is an attribute that can save the template, you only need to set this attribute to true. // avoid popping up normal. the dot used dialog box automatically saves the template wordapp. normaltemplate. saved = true; below is the main code // get the text content of the Word file
Public String word2text (string docfilename)
{
// Instantiate COM
Microsoft. Office. InterOP. Word. applicationclass wordapp = new Microsoft. Office. InterOP. Word. applicationclass ();
Object fileobj = docfilename;
Object nullobj = system. reflection. Missing. value;
// Open the specified file (the number of COM parameters varies in different versions. Generally, nullobj is used except the first one)
Microsoft. Office. InterOP. Word. _ document DOC = wordapp. Documents. Open (ref fileobj, ref nullobj, ref nullobj,
Ref nullobj, ref nullobj, ref nullobj,
Ref nullobj, ref nullobj, ref nullobj,
Ref nullobj, ref nullobj, ref nullobj
);
// Obtain the text in the DOC file
String outtext = Doc. content. text;
// Avoid the pop-up dialog box where the file (normal. Dot) is used and the template is automatically saved.
Wordapp. normaltemplate. Saved = true;
// Close the file
Doc. Close (ref nullobj, ref nullobj, ref nullobj );
// Close com
Wordapp. Quit (ref nullobj, ref nullobj, ref nullobj );
// Return
Return outtext;
}
Related Article

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.