2013-12-18 74 views
2

我發現@mkingston提供此解決方案: How to intercept and manipulate a Internet Explorer popup with VBA獲取IE窗口對象的窗口標題用VBA

...但它不是爲我工作。我已經添加了兩個有問題的參考庫,但是當我運行腳本時遇到以下問題:

編譯錯誤:未定義由於pauseUntilIEReady子(因爲此Sub未包括在答案中,我刪除它從腳本)

編譯錯誤:參數不可選由於oGetIEWindowFromTitle(所以我嘗試commeting這一點得到腳本編譯)

劇本終於編譯後,出現此錯誤:

自動化錯誤 系統找不到指定的文件。

在這行代碼

: 對於每個oGetIEWindowFromTitle在objShellWindows

這裏是我試圖運行代碼:

Function oGetIEWindowFromTitle(sTitle As String, _ 
          Optional bCaseSensitive As Boolean = False, _ 
          Optional bExact As Boolean = False) As SHDocVw.InternetExplorer 

Dim objShellWindows As New SHDocVw.ShellWindows 
Dim found As Boolean 
Dim startTime As Single 

found = False 
'Loop through shell windows 
For Each oGetIEWindowFromTitle In objShellWindows 
    found = oGetIEWindowFromTitleHandler(oGetIEWindowFromTitle, sTitle, bCaseSensitive, bExact) 
    If found Then Exit For 
Next 

'Check whether a window was found 
If Not found Then 
    Set oGetIEWindowFromTitle = Nothing 
Else 
    'COMMENTED OUT TO GET SCRIPT TO COMPILE pauseUntilIEReady oGetIEWindowFromTitle 
End If 

End Function 


Private Function oGetIEWindowFromTitleHandler(win As SHDocVw.InternetExplorer, _ 
            sTitle As String, _ 
            bCaseSensitive As Boolean, _ 
            bExact As Boolean) As Boolean 

oGetIEWindowFromTitleHandler = False 

On Error GoTo handler 
'If the document is of type HTMLDocument, it is an IE window 
If TypeName(win.Document) = "HTMLDocument" Then 
    'Check whether the title contains the passed title 
    If bExact Then 
     If (win.Document.title = sTitle) Or ((Not bCaseSensitive) And (LCase(sTitle) = LCase(win.Document.title))) Then oGetIEWindowFromTitleHandler = True 
    Else 
     If InStr(1, win.Document.title, sTitle) Or ((Not bCaseSensitive) And (InStr(1, LCase(win.Document.title), LCase(sTitle), vbTextCompare) <> 0)) Then oGetIEWindowFromTitleHandler = True 
    End If 
End If 
handler: 
'We assume here that if an error is raised it's because 
'the window is not of the correct type. Therefore we 
'simply ignore it and carry on. 

End Function 

Sub test() 

Dim ie As SHDocVw.InternetExplorer 
Dim doc As HTMLDocument 'If you have a reference to the HTML Object Library 
'Dim doc as Object 'If you do not have a reference to the HTML Object Library 

' Change the title here as required 
Set ie = oGetIEWindowFromTitle("My popup window") 
Set doc = ie.Document 

Debug.Print doc.getElementsByTagName("body").Item(0).innerText 

End Sub 
+1

發佈您正在嘗試使用的實際代碼。 –

+1

您好,我最初沒有發佈代碼,因爲它在鏈接解決方案中的建議是一樣的。我在下面複製它,其中一行註釋了我必須評論才能獲得編譯代碼。 – icfireball

回答

4

這對我的作品。

Function IEWindowFromTitle(sTitle As String) As SHDocVw.InternetExplorer 

    Dim objShellWindows As New SHDocVw.ShellWindows 
    Dim win As Object, rv As SHDocVw.InternetExplorer 

    For Each win In objShellWindows 
     If TypeName(win.Document) = "HTMLDocument" Then 
      If UCase(win.Document.Title) = UCase(sTitle) Then 
       Set rv = win 
       Exit For 
      End If 
     End If 
    Next 

    Set IEWindowFromTitle = rv 

End Function 

Sub Tester() 

    Dim w As SHDocVw.InternetExplorer 
    Set w = IEWindowFromTitle("Google") 
    If Not w Is Nothing Then 
     Debug.Print w.Document.Title 
    Else 
     Debug.Print "Not found" 
    End If 

End Sub 
+0

感謝您的回覆Tim。當我運行時,得到:「自動化錯誤/系統找不到指定的文件」與VBA打破在這一行:「For Each win In objShellWindows」 – icfireball

+0

適合我工作。 Win7 64位上的32位Excel。 –

+0

在兩臺運行Win7的不同計算機上安裝Excel 2010。我可能需要什麼參考庫,而不是Microsoft Internet控件和Microsoft HTML對象庫? – icfireball