Reputation: 1
I am getting this error "method document of object iwebbrowser2 failed".. Please look the code below.
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
Upvotes: 0
Views: 4979
Reputation: 3141
First make sure you have opened a site on Internet Explorer. Then run the code. I did not get the error as yours but got object reference error. So, added a check when the object is nothing.
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
Upvotes: 0