IE.LocationURL returns 错误 URL 未解决

IE.LocationURL returns error when URL not resolved

我有一个基本的 VBA 脚本可以打开 InternetExplorer window 以便用户可以授权 OAuth2.0 连接。作为此过程的一部分,他们的浏览器被重定向到包含访问令牌的回调地址。目前,我将重定向 URL 设置为 http://localhost.

脚本在 Windows 10 和 MS Access 2010 和 Internet Explorer 11 上正常运行。但是,在另一台 Win 10 PC 上的 MS Access 2016 中,我收到错误 -2147023179 Automation error The interface is unknown.。我已经确认重定向 URL 是正确的,我还硬编码了一个可解析的重定向 URL,它也有效。

似乎当 InternetExplorer 遇到无法解析的 URL 时,它会中断与 IE 实例的 VBA 连接。我正在寻找可以帮助我规避此问题的信息。

下面的代码是我正在使用的:

Dim IE as InternetExplorer

' create IE instance
Set IE = CreateObject("InternetExplorer.Application")

Dim url as String: url = "www.some-website.com"

' show IE
IE.Visible = True

' navigate to OAuth authorize URl
IE.Navigate url

' wait for page to load
Do While IE.ReadyState = 4: DoEvents: Loop

' loop until callback url in address bar
' ERROR HAPPENS HERE
While InStr(1, IE.LocationURL, "http://localhost") <= 0
    DoEvents
Wend

请尝试使用 InternetExplorerMedium 对象而不是 InternetExplorer 对象。更多详细信息,您可以查看以下links:

Excel VBA Controlling IE local intranet

当使用 InternetExplorerMedium 对象时,您可能需要添加对 Microsoft Internet Controls 的引用,您可以查看此 link:

How to add VBA References – Internet Controls, HTML Object Library