对象iwebbrowser2的方法文档失败

时间:2016-09-14 08:53:03

标签: vba excel-vba excel

我收到此错误"对象iwebbrowser2的方法文档失败" ..请查看下面的代码。

Sub getIE()
Dim sh As Object, oWin As Object, IE As Object

Set sh = CreateObject("Shell.Application")

For Each oWin In sh.Windows
    If TypeName(oWin.document) = "HTMLDocument" Then
        Set IE = oWin
        Exit For
    End If
Next

MsgBox IE.document.URL

End Sub

1 个答案:

答案 0 :(得分:0)

首先确保您已在Internet Explorer上打开网站。然后运行代码。我没有得到你的错误,但有对象引用错误。所以,当对象什么都没有时添加一个检查。

Sub getIE()
  Dim sh As Object, oWin As Object, IE As Object

  Set sh = CreateObject("Shell.Application")

  For Each oWin In sh.Windows
    If TypeName(oWin.document) = "HTMLDocument" Then
        Set IE = oWin
        Exit For
    End If
  Next

  If Not IE Is Nothing Then
    MsgBox IE.document.URL
  End If

End Sub