问题描述
在Excel中使用 MSHTML
时,是否可以将 HTMLDocument
强制设置为特定的 documentMode
?
Can HTMLDocument
be forced to a specific documentMode
when using MSHTML
in Excel?
到目前为止,与此相关的所有属性和方法似乎仅返回值,并且无法设置(例如documentMode,compatMode,兼容).
So far, all properties and methods related to this seem to only return values and cannot be set (ex. documentMode, compatMode, compatible).
在抓取和解析HTML的同时,我在组织中其他计算机上的Excel中却得到了不同的行为,这就是为什么我要尽可能地标准化.
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.
代码:
Dim doc As HTMLDocument
Set doc = New HTMLDocument
Debug.Print "compatMode: " & doc.compatMode
Debug.Print "documentMode: " & doc.documentMode
我的机器:
compatMode: BackCompat
documentMode: 11
其他机器:
compatMode: BackCompat
documentMode: 5
对于我比较过的系统,其操作系统版本和MS Office(O365)版本与我的机器相同.我还比较了与我的计算机相同的 msxml3.dll
和 msxml6.dll
的版本.
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.
推荐答案
- 我使用InternetExplorer的实例代替MSXML2.XMLHTTP.6.0
- 我没有使用MSHTML实例化各种类,而是仅使用通用的Object类.从MSHTML实例化任何内容都会引入不同的documentMode.
代码示例:
'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
这篇关于使用MSHTML.HTMLDocument时强制documentMode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!