2016-09-14 143 views
0

我得到這個錯誤「對象iwebbrowser2失敗的方法文件」..請看下面的代碼。對象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 
+0

檢查您的所有Internet Explorer(或Chrome)窗口是否關閉 –

回答

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 
相關問題