I'm using VBA in Excel to automate opening up a website, entering credentials, navigating to a specific page, then clicking on a link that downloads a file. When I do this, the following download dialog pops up, rather than getting the file download bar at the bottom:
The code I have pieced together from various resources works to get me all the way here but I can't for the life of me figure out how to automate the process of selecting "Save". I have tried sendkeys to no avail. I've tried using https://stackoverflow.com/a/32152712/5106668 but I can't get it to work either and it seems to be geared more towards instances where the file download bar pops up at the bottom. If I add a debug.print to get the value of h in the immediate window, it comes up as 0.
Here's the code I have:
Sub UserData_Export()
Dim ieApp As Object
Dim ieDoc As Object
Dim path As String
'create a new instance of ie
Set ieApp = New InternetExplorer
'you don’t need this, but it’s good for debugging
ieApp.Visible = True
'assume we’re not logged in and just go directly to the login page
ieApp.Navigate "https://login.XXXXXXXXXX.com/login"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Set ieDoc = ieApp.Document
'fill in the login form – View Source from your browser to get the control names
With ieDoc.forms(0)
.UserName.Value = "USERNAME"
.Password.Value = "PASSWORD"
.submit
End With
Application.Wait (Now + TimeValue("0:00:05"))
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
'now that we’re in, go to the page we want
ieApp.Navigate "https://XXXXXXXXXX.com"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
'simulate clicking "Export Users" link
ieApp.Navigate "javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(" & Chr(34) & "ctl00$ctl00$ctl00$FormContentPlaceHolder$MainContentPlaceHolder$RightColumnPlaceHolder$ExportUsersLinkButton" & Chr(34) & ", " & Chr(34) & Chr(34) & ", true, " & Chr(34) & Chr(34) & ", " & Chr(34) & Chr(34) & ", false, true))"
'Wait for page to load
Do While ieApp.Busy Or Not ieApp.ReadyState = READYSTATE_COMPLETE
DoEvents
Loop
' SAVE DIALOG HAS POPPED UP AT THIS POINT, CODE TO CLICK SAVE NEEDS TO GO HERE!!
'close 'er up
ieApp.Quit
Set ieApp = Nothing
End Sub