【发布时间】:2017-07-19 10:18:50
【问题描述】:
我实现了几个依赖于从某些网站下载一些信息的功能。
此类函数的最简单示例是:
Public Function getSomething(webAddress As String)
Dim html As HTMLObjectElement
Set html = getWebContents(webAddress)
Set elems = html.body.getElementsByTagName(tagName)
...
End Function
从网站获取数据的功能是:
Public Function getWebContents(webAddress As String) As HTMLObjectElement
Dim ie As InternetExplorer
Dim html As HTMLDocument
Set ie = New InternetExplorer
ie.Visible = False
ie.Navigate webAddress
Do While ie.READYSTATE <> READYSTATE_COMPLETE
Application.StatusBar = "Trying ..."
DoEvents
Loop
Set getWebContents = ie.Document
'close down IE and reset status bar
'ie.Quit
Set ie = Nothing
Application.StatusBar = ""
End Function
问题是我似乎需要取消注释 ie.Quit 行来关闭 IE 实例。但是当我取消注释时 ie.Quit the line
Set elems = html.body.getElementsByTagName(tagName)
产生错误。
退出 IE 后,我似乎无法使用函数 getWebContents 返回的 HTMLObjectElement。如何处理?我可以在 getSomething 函数中实现 try...finally 块,然后在 finally 块中打开 ie 并关闭。但是,我有许多类似性质的功能,并进行了许多类似的尝试...finally 块似乎是一个愚蠢的想法。
有什么想法吗? 谢谢!
【问题讨论】:
标签: windows vba internet-explorer