【问题标题】:Bring Internet Explorer Download Window to Focus - Foreground via VBA使 Internet Explorer 下载窗口成为焦点 - 通过 VBA 的前景
【发布时间】:2020-10-28 03:52:37
【问题描述】:

我有一个当前可以工作的宏,但我正在尝试自动化我到现在为止不得不忍受的最后一个“怪癖”。基本上它打开 IE 到一个 URL,然后关闭窗口并返回一个下载窗口,我有 excel 使用 SENDKEYS 下载文件。

我正在努力使下载窗口成为焦点,目前我的最终用户单击 DL 窗口以使 sendkeys 按预期工作。

我已阅读以下内容并尝试使用该代码无济于事:

vbscript - Bring Internet Explorer Application window to front

VBA to Activate Internet Explorer Window

Bringing Internet Explorer window to foreground

Set Focus to Internet Explorer Object in Visual Basic

有几点需要注意:

  • 我无法批量下载文件,因为我需要 IE 来传递凭据。
  • 文件不是静态的,URL 在后端运行脚本,然后将文件呈现给终端终端
  • 我不能要求在最终用户计算机上“启用参考库”

宏如下:

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 窗口底部显示提示,而是关闭该窗口并显示如下所示的“完整下载窗口”。

【问题讨论】:

    标签: vba internet-explorer


    【解决方案1】:

    我最终做的是使用ObjURL 打开IE 实例,然后使用DLUrl 打开它。这做了一些重要的调整,它类似于在 IE 中打开第二个选项卡,它会关闭,但不是使用“下载窗口”,而是使用 IE 窗口底部的提示。这让我可以继续控制IEObj,因为原来的ObjURL 仍然打开。我无法让 excel VBA 始终如一地控制“下载窗口”,但我能够使用以下 sn-ps 获得一致的结果。在这两次调整之间,我现在得到了一致的结果。

    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
    

    【讨论】:

      【解决方案2】:

      由于“查看下载”窗口在IE中的快捷键是Ctrl+J,所以我想我们可以使用sendkeys点击Ctrl+J来把窗口放到前面。 p>

      示例代码:

      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
      

      结果:

      【讨论】:

      • 好的。如果问题仍然存在,请随时返回。
      • 您是否只想将“查看下载”窗口置于最前面?如果是这样,请查看我的编辑。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-26
      • 2018-11-14
      • 2019-11-01
      相关资源
      最近更新 更多