【发布时间】:2020-12-18 13:56:04
【问题描述】:
这是在清理我的 InternetExplorer 实例时返回 HTMLDocument 的有效方法吗?
Private Function myFunction() As HTMLDocument
'init
Dim out As HTMLDocument
Dim browser As InternetExplorer
Set browser = CreateObject("InternetExplorer.Application")
...
'get HTMLDocument
Set out = browser.document
browser.Quit
Set browser = Nothing
Set myfunction = out
Exit Function
我之所以这么问,是因为稍后在我的代码中使用返回的 HTMLDocument 会出现不稳定的行为。我会看到输出003,但不会看到004:
doc = myFunction()
...
Debug.Print "003"
strVar = doc.getElementsByClassName("sectionheading_center")(0).innerText
Debug.Print "004"
....
错误:-2147417848-Automation error The object invoked has disconnected from its clients. -VBAProject-1000440-0
【问题讨论】:
-
不像这样,因为您正在发布浏览器参考。创建一个包装类,在其中实例化
Intitialize()上的浏览器并清除Terminate()。
标签: excel vba internet-explorer