Forcing documentMode when using MSHTML.HTMLDocument

断了今生、忘了曾经 提交于 2021-02-11 13:01:46

问题


Can HTMLDocument be forced to a specific documentMode when using MSHTML in Excel?

So far, all properties and methods related to this seem to only return values and cannot be set (ex. documentMode, compatMode, compatible).

While scraping and parsing HTML, I'm getting different behaviours in Excel on other machines in the organization which is why I want to standardize as much as I can.

Code:

Dim doc As HTMLDocument
Set doc = New HTMLDocument

Debug.Print "compatMode: " & doc.compatMode
Debug.Print "documentMode: " & doc.documentMode

My machine:

compatMode: BackCompat
documentMode: 11

Other machines:

compatMode: BackCompat
documentMode: 5

For the systems I compared with, the OS builds and MS Office (O365) versions were the same as my machine. I also compared the version of msxml3.dll and msxml6.dll which were also the same with my machine.


回答1:


  1. Instead of MSXML2.XMLHTTP.6.0, I used an instance of InternetExplorer
  2. Instead of instantiating various classes from MSHTML, I simply used the generic Object class. Instantiating anything from MSHTML would introduce different documentModes.

Code example:

'Get document using IE
Dim doc As HTMLDocument
...
set doc = ie.Document

'Old - Extracting rows
Dim element As MSHTML.HTMLGenericElement
For Each element In tableRows

'New - Extracting rows
Dim element As Object
For Each element In tableRows


来源:https://stackoverflow.com/questions/64500359/forcing-documentmode-when-using-mshtml-htmldocument

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!