How to get mshtml.IHTMLDocument6 or mshtml.IHTMLDocument7?

后端 未结 1 1568
[愿得一人]
[愿得一人] 2020-12-06 14:41

I am using IE11 in Windows 7. Then I added a reference in C# project c:\\Windows\\System32\\mshtml.tld and try to get mshtml.IHTMLDocument6 or mshtml.IHTMLDocument7

相关标签:
1条回答
  • 2020-12-06 15:22

    Select the MSHTML assembly reference, look at its Path property and you'll see the problem:

    C:\WINDOWS\assembly\GAC\Microsoft.mshtml\7.0.3300.0__b03f5f7f11d50a3a\Microsoft.mshtml.dll

    This is the PIA for the mshtml type library, installed on any programmer's machine that has VS installed. Version 7.0.3300 is a version number from the .NET 1.0 era, it is 14 years old. Predates IE11 of course, that's why it doesn't have the later interface types. Using the PIA is never the wrong thing to do, it ensures that your program will run with any version of IE, all the way back to IE6.

    But that's not what you want, you'll have to generate your own interop library from the IE11 type library. Run the Visual Studio Command Prompt, navigate to your project directory and type this command:

      Tlbimp c:\windows\system32\mshtml.tlb /out:Interop.mshtml.dll
    

    It will trundle for quite a while, it is a big one, and spit out several warnings about the PIA being present and several obscure methods and types that cannot be directly used from a .NET program. You can ignore them, they didn't work in the PIA either.

    You now have the Interop.mshtml.dll file in your project directory. Back to VS, delete the old MSHTML reference and use Project > Add Reference > Browse to select the new one you just created. Its "Embed Interop Types" property is True by default, no longer a need for a PIA at all and you don't have to deploy the file with your program. Check the file into source control so you only have to do it once.

    And be careful, your program can die with an E_NOINTERFACE exception when it runs on machine that doesn't have the required version of IE installed. You can use the as operator to cast the document reference to IHtmlDocument7, it will be null if IE is old.

    0 讨论(0)
提交回复
热议问题