How. Net calls the Office COM Component

Source: Internet
Author: User

To use the wordtohtml method today, you must add a reference to the office component (Microsoft. Office. InterOP. Word)

Assembly Microsoft. Office. InterOP. Word
D: \ Program Files \ Microsoft Visual Studio 9.0 \ Visual Studio Tools for office \ Pia \ office12 \ microsoft. Office. InterOP. Word. dll

The call code is as follows:

Using Microsoft. Office. InterOP. word;

Applicationclass word = new applicationclass ();
Type wordtype = word. GetType ();
Documents docs = word. documents;

// Open the file
Type docstype = docs. GetType ();
Document Doc = (document) docstype. invokemember ("open ",
System. reflection. bindingflags. invokemethod, null, Docs, new object [] {wordfilepath, true, true });

// Convert the format and save it
Type doctype = Doc. GetType ();

Doctype. invokemember ("saveas", system. reflection. bindingflags. invokemethod,
Null, Doc, new object [] {htmlfileurl, wdsaveformat. wdformatfilteredhtml });

Doctype. invokemember ("close", system. reflection. bindingflags. invokemethod,
Null, Doc, null );

// Exit word
Wordtype. invokemember ("quit", system. reflection. bindingflags. invokemethod,
Null, word, null );

The principle is obvious: Start the word.exe program through the program, open the local file, save it as HTML, close the Word document, and exit the word program.

After the program is executed, there are two files, the original Word file and the HTML file.

There is no problem with local debugging. Windows2003 + vs2008 + iis6.0 + office2007

Publish a website. After the website is configured on the local IIS (the read and write permissions are checked), an error occurred while converting the uploaded Word file. The error message is as follows:

Failed to retrieve components whose CLSID is {00024500-0000-0000-c000-000000000046} in the com class factory because of the following error: 80070005.

The answer for searching posts from the Internet is roughly the same: the login Website user does not have the permission to operate word.exe

Run "input dcomcnfg.exe to start" component service ". You can also enter it from the management tool,

"Component Service"-> "computer"-> "my computer"-> "DCOM configuration"-> find the word-> properties-> "ID" tag, select "Interactive User"

-> Security "tag"-> click "Custom" on "Start and activate Permissions", and then click the corresponding "edit" button, in the pop-up "security" dialog box, add a "Network Service" user (note that you must select the computer name) and grant it the "local start" and "Local activation" permissions.

-> "Security" label, click "Custom" on "Access Permissions", and then click "edit ", in the pop-up "security" dialog box, add a "Network Service" user and grant "local access" permission. in this way, we have configured the corresponding Excel DCOM permissions.

Restart IIS on the local machine. if the machine is not restarted, open the website for testing. OK. By the way, you must have the read and write permissions for the Word files and HTML folders to be operated, right-click -- Security -- everyone or network service user --> check the Read and Write Permissions

Next, publish it to the server, test the Internet login, and throw the Yellow Pages, prompting that the running error is not prompted because it is not the logon user of the local machine.

Well, I browsed the test from the IIS on the server and threw out the same error as on my machine. It's great. Now I can configure the DCOM permission by my trainer and open the component service dumb.

On the server, windows2008 + iis7.0, no word is found in the DCOM configuration. Check whether the Office is installed. The result is that it is installed, office2007, and search for the post windows2008,

I found a few posts and found that XP's component service configuration is similar to that of winserver2008.

"Component Service"-> "computer"-> "my computer"-> properties --> COM components-> enable and activate permissions are set as above-> access permission editing is the same as above-> All added a network service user to grant the user the corresponding read and write permissions.

Restart IIS and look forward to the same test error. Restart the server, the same result! Depend on me

Did the user add an error? Is it an ASPnet user? But the Post says that window2000 is the user, iis5.0 is the user, but Windows + iis7.0 is the server.

Continue modification and restart, still fruitless

There are still some tricks on the Internet: Although I have not tried it, I will post it first.

In the web. use identity simulation in config, in <system. in the Web> section, add <identity impersonate = "true" username = "your username" Password = "password"/> </system. web>

 

I have been wondering why the Office Word and office family are not found in the DCOM configuration on the server. Is vs2008 installed a Framework version of a series of components that do not automatically register the office?

I have studied registration of some COM components and registration of office, and understood why I said *. DLL (Dynamic Class Library) and *. EXE files are all executable files, and some solutions on the Internet are Excel files. paste them:

Registration Method executionStart ---- run ---- enter the installation path of excel2003, for example, "C: \ Program Files \ Microsoft Office \ office11 \ excel.exe"/regserverNote/there is a space before the symbol. "C: \ Program Files \ Microsoft Office \ office11 \ excel.exe" is the path of the installation program of excel2003. The regserver parameter indicates registration!
After running, the installation program of Office 2003 will be started, repaired, and re-registered.
Excel2007 uses the same method as the default startup program and can be executed.Start ---- run ---- "D: \ Program Files \ Microsoft Office \ office12 \ excel.exe"/regserver
In summary, when you encounter Excel export and import problems, consider the permission issue first, and then consider the Office version environment! This can basically solve these problems.

Good! Tomorrow I will have a try!

<-- By the way, the post author's experience: I have also encountered errors when using the above methods. My environment is Windows2003 + office2003 + office2007, you can export data on the test server! However, if the official environment is updated, an error is returned. If all permissions are set, an error is returned. Because my test server is started by the default Office, I suspect it is an office environment problem! So I uninstalled office2007 and immediately tested it. But after I restarted the server, the have not been pre-compiled error occurred, and the export page could not be opened, so I re-installed office2007 and opened the page, but an error was reported when exporting the Excel file! I reset all the permissions again. the problem persists! Later, I found that the Excel programs opened by the two servers are different by default. The test environment is opened by office2003 by default, and the official environment is opened by office2007. So I re-registered office2003 in the official environment, so that it is also the default office2003 open program, rely on! The problem is finally solved ---->

 

 

 

 

 

 

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.