2

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:

Download Dialog

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
  • I seem to remember that when the save dialog is up either .Busy remains true or .ReadyState doesn't reach READYSTATE_COMPLETE. Can't remember which, but probably .Busy. –  Aug 03 '18 at 20:05
  • The code I had attempted to use to click the save button was able to partially execute so I don't know if the ReadyState would be a problem here. – Marc Fernandes Aug 03 '18 at 20:11
  • Is there an URL associated with the download you could use for a direct download ? I am guessing unlikely if login based. – QHarr Aug 03 '18 at 20:20
  • I wish there was! The code marked "simulate clicking 'Export Users link" shows how I click the link to bring up the save as dialog box. Unfortunately it's not a direct download. – Marc Fernandes Aug 03 '18 at 20:27
  • 1
    Could you please share the URL of the webpage? You may check using Fiddler if there is any XHR is made when you click and download the file manually in the browser, and if so, reproduce that XHR from VBA code then. – omegastripes Aug 03 '18 at 21:03
  • Unfortunately I can't share the website as it's a company training system. I have a tenuous grasp of programming and haven't used fiddler before, can you tell me generally how I would do this? – Marc Fernandes Aug 03 '18 at 23:08
  • See the following list: https://www.google.co.uk/search?q=youtube+fiddler+telerik&oq=youtube+fiddler+te&aqs=chrome.1.69i57j0l3j69i64.11125j0j7&sourceid=chrome&ie=UTF-8 You can also watch with Chrome dev tools by pressing F12 in chrome when doing the above process. And for XHR see https://codingislove.com/http-requests-excel-vba/ as a starting point. – QHarr Aug 04 '18 at 05:27
  • Did you manged to solve the issue? – rosuandreimihai Dec 22 '18 at 19:49

0 Answers0