can someone help me or at least point me in the right direction.
I am trying to write a script automation in VBA that will go into a website and hit some buttons and checkboxes on that website that then start the download.
The webpage uses ASP.net.
I can achieve exactly what I want using the InternetExplorer.Applicaion object, but the problem is saving the file, since I use the API and SendKeys in the SaveAs dialog box, it works, but it’s very rude and it can easily crash, and I really want to it could work on its own without any problems.
Ideally, I would like to be able to use an HTTP request / response, but I tried for several hours without any success due to the authentication of the sites, and I also think that it has some protection from scripts since the cookie has HttpOnly flag set.
The file I want to download is text / csv, and the received data can be accessed through the HTTP response header, which I can see through Fiddler.
Is there a way to get the HTTP response header from the InternetExplorer.Applicaion object, I looked at the MS documentation and I don't see the obvious way to get to this?
Here is the code I'm currently using:
Private Function GetFile() As Scripting.File On Error Resume Next Dim objFso As Scripting.FileSystemObject: Set objFso = New Scripting.FileSystemObject Dim strFileName As String: strFileName = Year(Now()) & Month(Now()) & Day(Now()) & Hour(Now()) & Minute(Now()) & Second(Now()) & ".csv" Dim strFolder As String: strFolder = objFso.GetAbsolutePathName(Environ("TEMP")) & "\" Dim strSaveAsFullPath As String: strSaveAsFullPath = strFolder & strFileName Dim objIE As InternetExplorer: Set objIE = New InternetExplorer With objIE .Visible = True .Navigate2 "WEB URL 1" Do Until Not .Busy Application.Wait Now + TimeValue("00:00:01") DoEvents Loop .Document.getElementById("ctl02_txtUserId").value = "USER" .Document.getElementById("ctl02_txtPassword").value = "PASS" .Document.getElementById("ctl02_btnLogon").Click Do Until Not .Busy Application.Wait Now + TimeValue("00:00:01") DoEvents Loop .Navigate2 "WEB URL 2" Do Until Not .Busy Application.Wait Now + TimeValue("00:00:01") DoEvents Loop .Document.getElementById("ddlTables").selectedIndex = 8 .Document.forms(0).submit Do Until Not .Busy Application.Wait Now + TimeValue("00:00:01") Loop .Document.getElementById("gvFields_lnkbtnSelectAll").Click Do Until Not .Busy Application.Wait Now + TimeValue("00:00:01") DoEvents Loop .Document.getElementById("btnRetrieveData").Click Do Until Not .Busy Application.Wait Now + TimeValue("00:00:01") DoEvents Loop .Document.getElementById("btnRetrieveData").Focus
'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '"" "" "" "" "" "" "THIS WHERE I DOWNLOADED THE FILE USING SENDKEYS :(" DO I NEED THE BEST SOLUTION PLEASE? "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "'' '' '' ''
Application.Wait Now + TimeValue("00:00:01") DoEvents Application.SendKeys "{TAB}", True Application.SendKeys "{TAB}", True Application.SendKeys "{DOWN}", True Application.SendKeys "{DOWN}", True Application.SendKeys "a", True Application.Wait Now + TimeValue("00:00:01") DoEvents Application.SendKeys strSaveAsFullPath, True Application.SendKeys "{TAB}", True Application.SendKeys "{TAB}", True Application.SendKeys "{TAB}", True Application.Wait Now + TimeValue("00:00:01") DoEvents Application.SendKeys "s", True Application.Wait Now + TimeValue("00:00:02") Application.SendKeys "y", True
'' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ' '' '' '' '' '
.Quit End With Dim objFile As Scripting.File: Set objFile = objFso.GetFile(strSaveAsFullPath) Set GetTransworldFile = objFile Set objIE = Nothing Set objFso = Nothing
Final function