[英]Bring Internet Explorer Download Window to Focus - Foreground via VBA
我有一個目前可以工作的宏,但我正在嘗試自動化我到現在為止不得不忍受的最后一個“怪癖”。 Basically it opens IE to a URL which then closes the window and spits back a download window and I have excel use SENDKEYS
to download file.
我正在努力使下載 window 成為焦點,目前我的最終用戶單擊 DL window 以使 sendkeys 按預期工作。
我已閱讀以下內容並嘗試使用該代碼無濟於事:
vbscript - 將 Internet Explorer 應用程序 window 放在前面
VBA 激活 Internet Explorer Window
將 Internet Explorer window 帶到前台
在 Visual Basic 中將焦點設置為 Internet Explorer Object
有幾點需要注意:
宏如下:
Public Sub DLFILE()
'This will load a webpage in IE
Dim i As Long
Dim URL As String
Dim ie As Object
Dim objElement As Object
Dim objCollection As Object
'Create InternetExplorer Object
Set ie = CreateObject("InternetExplorer.Application")
'Define URL
URL = "http://example.com/"
'Navigate to URL
ie.navigate URL
' Statusbar let's user know website is loading
Application.StatusBar = URL & " is loading. Please wait..."
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While ie.ReadyState = 4: DoEvents: Loop 'Do While
Application.Wait (Now() + TimeValue("00:00:04"))
SendKeys "{RIGHT}{RIGHT}{ENTER}{TAB}{TAB}{TAB}{TAB}{ENTER}"
'Do Until IE.ReadyState = 4: DoEvents: Loop 'Do Until
'Unload IE
Set ie = Nothing
Set objElement = Nothing
Set objCollection = Nothing
End Sub
另請注意,這不會在 IE window 底部顯示提示,但會關閉 window 並顯示如下所示的“完整下載窗口”。
由於“查看下載”的快捷方式 window 在 IE 中是Ctrl+J ,所以我認為我們可以使用發送鍵單擊 Ctrl+J 將sendkeys
帶到前面。
示例代碼:
Sub LOADIE()
Set ieA = CreateObject("InternetExplorer.Application")
ieA.Visible = True
ieA.navigate "https://www.bing.com/"
Do Until ieA.readyState = 4
DoEvents
Loop
Application.Wait (Now + TimeValue("00:00:03"))
Application.SendKeys "^{j}"
End Sub
結果:
我最終做的是使用ObjURL
打開 IE 實例,然后使用DLUrl
打開它。 這進行了一些重要的調整,類似於在 IE 中打開第二個選項卡,該選項卡將關閉,但不是使用“下載窗口”,而是使用 IE Window 底部的提示。 這使我能夠繼續控制IEObj
,因為原始ObjURL
仍處於打開狀態。 我無法讓 excel VBA 始終如一地控制“下載窗口”,但我能夠使用以下代碼段獲得一致的結果。 在這兩次調整之間,我現在有一致的結果。
Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long
和
'Bring IEObj to Focus
HWNDSrc = IEObj.HWND
SetForegroundWindow HWNDSrc
完整的子:
Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long
Public Sub DL_File_IE()
Dim DLUrl As String, ObjURL As String
Dim IEObj As Object, objElement As Object, objCollection As Object
Set IEObj = CreateObject("InternetExplorer.Application")
IEObj.Visible = True
ObjURL = "http://google.com"
DLUrl = "http://example.com/file
IEObj.navigate ObjURL
Do Until IEObj.readyState = 4
DoEvents
Loop
IEObj.navigate DLUrl
'Bring IEObj to Focus
HWNDSrc = IEObj.HWND
SetForegroundWindow HWNDSrc
Application.Wait (Now() + TimeValue("00:00:02"))
SendKeys "{TAB}{TAB}{ENTER}", True ' Select "Save" in DL Window
Application.Wait (Now() + TimeValue("00:00:02"))
SendKeys "%{f4}", True ' Alt + F4 = Close IEObj
'Unload IE
Set IEObj = Nothing
Set objElement = Nothing
Set objCollection = Nothing
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.