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.
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.
I have read through the following and tried utilizing the code to no avail:
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
There a few things to note:
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.
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.
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. 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. This allowed me to continue to control the IEObj
as the original ObjURL
was still open. I couldn't get excel VBA to consistently control the "Downloads Window", but I was able to get consistent results using the following snippets. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.