【发布时间】: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