none
System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' RRS feed

  • Question

  • Hi

    Another on of these InvalidCastException, ie.,System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to class type 'mshtml.HTMLWindow2Class'. COM components that ...

    From reading some of the past postings, installing Office PIA seems to fix the problem.

    Can anyone suggest is there another way to fix this problem without asking the users to install PIA? Maybe for example using something else other than mshtml.HTMLWindow2Class. The offending code is

        mshtml.IHTMLDocument doc = (mshtml.IHTMLDocument)this.axWebBrowser1.Document;
        window = (mshtml.HTMLWindow2Class)doc.Script;

    Help is much appreciated.

    regards
    benc
    Sunday, August 24, 2008 7:42 AM

Answers

  • Select the MSHTML reference in the References node, then set Copy Local = true in the Properties window.  Compile.  Now you've got Microsoft.mshtml.dll in your build folder.  Deploy it with your final .exe to avoid taking a dependency on the PIA.  Not quite sure if that is really a good idea, your program could break when MSFT updates c:\windows\system32\mshtml.dll.  At least check it with the IE8 beta on a non-production machine.
    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Wednesday, August 27, 2008 10:24 AM
    Sunday, August 24, 2008 6:34 PM
    Moderator

All replies

  • Select the MSHTML reference in the References node, then set Copy Local = true in the Properties window.  Compile.  Now you've got Microsoft.mshtml.dll in your build folder.  Deploy it with your final .exe to avoid taking a dependency on the PIA.  Not quite sure if that is really a good idea, your program could break when MSFT updates c:\windows\system32\mshtml.dll.  At least check it with the IE8 beta on a non-production machine.
    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Wednesday, August 27, 2008 10:24 AM
    Sunday, August 24, 2008 6:34 PM
    Moderator
  • Didn't seem to work. Manually copied the Microsoft.mshtml.dll over to the .exe folder of the user, but the same invalid cast exception problem still exist. Isn't this the same as you suggested?

    Will try to recompile code using HTMLWindow2 (if there exist one, as highlighted in another posting) instead of HTMLWindow2Class and see if it works.

    Wednesday, August 27, 2008 2:24 PM