我有一个宏,它目前工作,但我试图自动化的最后“怪癖”,我必须忍受到现在为止。基本上,它打开IE到一个URL,然后关闭窗口,吐回一个下载窗口,我有excel使用SENDKEYS
下载文件。
我正在努力使下载窗口聚焦,目前,我的最终用户点击DL窗口发送键工作的预期。
我已经阅读了以下内容,并尝试使用该代码,但没有结果:
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传递credentials.
上有任何“启用参考库”的要求
宏如下:
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窗口底部的提示符,而是关闭该窗口并弹出如下所示的“完整下载窗口”。
发布于 2020-07-13 20:29:58
我最后所做的是使用一个ObjURL
打开IE实例,然后打开一个DLUrl
。这做了一些重要的调整,它类似于打开IE中的第二个选项卡,该选项卡将关闭,但它将使用IE窗口底部的提示,而不是使用“下载窗口”。这允许我继续控制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
发布于 2020-07-08 03:28:34
由于“查看下载”窗口的快捷方式是IE中的 Ctrl+J ,所以我认为我们可以使用sendkeys
单击Ctrl+J将窗口放在最前面。
样本代码:
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
结果:
https://stackoverflow.com/questions/62782892
复制相似问题