开发者

Opening Word Document through code in Visual Studio C#

I am developing a Office Development with Visual Studio. And receive the error below

Error: 
**
Unable to cast COM object of type 'Microsoft.Office.Interop.Word.ApplicationClass' to interface type 'Microsoft.Office.Interop.Word._Application'. 
This operation failed because the QueryInterface call on the COM component for the interface with IID '{00020970-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
**

Code: (also at https://gist.github.com/1056809 )

if (File.Exists((string)strDocPath))
{
    Word.Application wdApp = new Word.Application();
    wdApp.Visible = true; //Error thrown here

    object readOnly = false;
    object isVisible = true;
    object oMissing = System.Reflection.Missing.Value;

    //Open the word document
    //Error thrown on line below.
    Word.Document aDoc = wdApp.Documents.Open(ref strDocPath, ref oMissing, ref readOnly, ref 开发者_运维技巧oMissing, ref oMissing, ref oMissing,
                                              ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref isVisible,
                                              ref oMissing, ref oMissing, ref oMissing, ref oMissing); 


    // Activate the document
    aDoc.Activate();
}

What is this error? How may I avoid it?


The problem is HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID{000209FF-0000-0000-C000-000000000046} cannot be registered. Although the class exists (Microsoft.Office.Interop.Word.ApplicationClass) in your registry, does not mean it has been registered. Microsoft doesn't allow the Microsoft.Office.Interop.Word.dll to be registered for some unexplainable reason and as a result, if you are referencing "ApplicationClass" class in your code; you'll run into this issue when deployed to an actual server. You won't get an error message/warning on your local/build machine. HERE's what the generic error may look like:

Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

Conclusion: Even if you have Microsoft Office 2007/2010 installed/activated & licensed. The "Microsoft.Office.Interop.Word.dll" dll cannot be registered. It took me almost an entire week to figure this out. NOTE: To see for yourself; download-install-run "RegDllView". You can see all the registered DLL's for "WINWORD.EXE". Take note, {000209FF-0000-0000-C000-000000000046} will not be displayed. Attempting to even manually registering the "Microsoft.Office.Interop.Word.dll" using the program will fail with an error code 127. I believe the work around is actually using the provided "Document" interface with-in the Microsoft.Office.Interop.Word.dll. It's simply just a wrapper for WINWORD.EXE is what it comes down to.


Try replacing your first line after the if statement with something like this:

Microsoft.Office.Interop.Word.Application wdApp = new Microsoft.Office.Interop.Word.Application();

Then make sure you add a reference to "Microsoft Word 12.0 Object Library" COM object, which will look like "Microsoft.Office.Interop.Word"in the Solution explorer.

I tested this and a blank MS Word application came up. So let's see if we can get that far.


I know it's a bit "overdue", but some might still struggle with finding an easy solution (like myself did), hence my reply.

As per @Marine_Elite post, the class is not registered correctly but there is a workaround (I think) much easier than writting a custom wrapper around WINWORD.EXE.

I've used the example taken from here and applied it to the MSWORD.OLB
In short, you'll probably need just to download the regtlibv12.exe and run it against the OLB mentioned above, but following all the steps from the provided link is highly recommended.

The finall command I've used is:
regtlibv12.exe "C:\Program Files\Microsoft Office\root\Office16\MSWORD.OLB"

Since then my code runs just fine, although I'm not sure how the application will behave on a different host or even on my own machine after a reboot. Registering the OLB once again in the latter scenario potential problems is a simple solution though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜