Opening Word Document through code in Visual Studio C#

10,176

Solution 1

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.

Solution 2

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.

Share:
10,176
slao
Author by

slao

Updated on June 04, 2022

Comments

  • slao
    slao almost 2 years

    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?

  • Nick Heidke
    Nick Heidke almost 13 years
    Can you try using this line instead of line 3 from your code?
  • slao
    slao almost 13 years
    There is an error does not recognized ApplicationClass. Gives the following error: Interop type 'Microsoft.Office.Interop.Word.ApplicationClass' cannot be embedded. Use the applicable interface instead. The type or namespace name 'ApplicationClass' could not be found (are you missing a using directive or an assembly reference?)
  • Nick Heidke
    Nick Heidke almost 13 years
    I'm wondering if you aren't referencing the correct COM object?
  • Alex Jolig
    Alex Jolig over 8 years
    What do you mean by using the provided "Document" interface with-in the Microsoft.Office.Interop.Word.dll ?
  • MindRoasterMir
    MindRoasterMir about 5 years
    this didnot work for me to import a word document. thanks anyway