简体   繁体   English

将 Internet Explorer 下载 Window 带到焦点 - 前景通过 VBA

[英]Bring Internet Explorer Download Window to Focus - Foreground via VBA

I've got a macro which currently works but I'm trying to automate the final "quirk" that I have had to live with up till now.我有一个目前可以工作的宏,但我正在尝试自动化我到现在为止不得不忍受的最后一个“怪癖”。 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. 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.

I'm struggling to bring the download window to focus, currently my end users click on the DL window for sendkeys to work as expected.我正在努力使下载 window 成为焦点,目前我的最终用户单击 DL window 以使 sendkeys 按预期工作。

I have read through the following and tried utilizing the code to no avail:我已阅读以下内容并尝试使用该代码无济于事:

vbscript - Bring Internet Explorer Application window to front vbscript - 将 Internet Explorer 应用程序 window 放在前面

VBA to Activate Internet Explorer Window VBA 激活 Internet Explorer Window

Bringing Internet Explorer window to foreground 将 Internet Explorer window 带到前台

Set Focus to Internet Explorer Object in Visual Basic 在 Visual Basic 中将焦点设置为 Internet Explorer Object

There a few things to note:有几点需要注意:

  • I can't download the file via batch as I need IE to pass along the credentials.我无法通过批处理下载文件,因为我需要 IE 来传递凭据。
  • The file is not static, the URL runs a script on the backend and then presents the file back to end terminal该文件不是 static,URL 在后端运行脚本,然后将文件返回到终端终端
  • I can't have any requirement to "Enable Reference Library" on end users computers我对最终用户计算机上的“启用参考库”没有任何要求

The macro is as follows:宏如下:

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

Also note, this doesn't bring up the prompt in the bottom of the IE window, but closes that window and brings up the "Full Downloads Window" like below.另请注意,这不会在 IE window 底部显示提示,但会关闭 window 并显示如下所示的“完整下载窗口”。

在此处输入图像描述

As the shortcut for the "View Downloads" window is Ctrl+J in IE so I think we can use sendkeys to click Ctrl+J to bring the window to the front.由于“查看下载”的快捷方式 window 在 IE 中是Ctrl+J ,所以我认为我们可以使用发送键单击 Ctrl+J 将sendkeys带到前面。

Sample code:示例代码:

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

Result:结果:

在此处输入图像描述

What I ended up doing was using an ObjURL to open the IE instance and then a DLUrl which I opened after.我最终做的是使用ObjURL打开 IE 实例,然后使用DLUrl打开它。 This made a few important adjustments, it's similar to opening a second tab in IE which would close, but instead of using the "Downloads Window" it would use the prompt at the bottom of the IE Window.这进行了一些重要的调整,类似于在 IE 中打开第二个选项卡,该选项卡将关闭,但不是使用“下载窗口”,而是使用 IE Window 底部的提示。 This allowed me to continue to control the IEObj as the original ObjURL was still open.这使我能够继续控制IEObj ,因为原始ObjURL仍处于打开状态。 I couldn't get excel VBA to consistently control the "Downloads Window", but I was able to get consistent results using the following snippets.我无法让 excel VBA 始终如一地控制“下载窗口”,但我能够使用以下代码段获得一致的结果。 Between the two adjustments I now have consistent results.在这两次调整之间,我现在有一致的结果。

Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long

And

'Bring IEObj to Focus
HWNDSrc = IEObj.HWND
SetForegroundWindow HWNDSrc

Full Sub:完整的子:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM