[英]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.