Hello,
Long time creeper, first time poster!
I've seen similar questions about IE automation on this board and need help adapting code I have found to my situation.
I have code that currently opens IE, navigates to the desired URL, interacts with the form by clicking checkboxes that fit my needs, and clicks a submit button. I then had code that would save the file locally. Overall this code was very dependent on Sleep and wait commands. I found code that looked more efficient on this thread:
https://www.mrexcel.com/forum/excel...n-internet-explorer-web-site.html#post3404965
I need help adapting the code, because although it saves a file, it does not save what I need (the csv file). It saves the HTML source code.
Note: the piece of the url that needs to be able to change is the n_grpclm_id depending on the query
Here's my current code:
Long time creeper, first time poster!
I've seen similar questions about IE automation on this board and need help adapting code I have found to my situation.
I have code that currently opens IE, navigates to the desired URL, interacts with the form by clicking checkboxes that fit my needs, and clicks a submit button. I then had code that would save the file locally. Overall this code was very dependent on Sleep and wait commands. I found code that looked more efficient on this thread:
https://www.mrexcel.com/forum/excel...n-internet-explorer-web-site.html#post3404965
I need help adapting the code, because although it saves a file, it does not save what I need (the csv file). It saves the HTML source code.
Note: the piece of the url that needs to be able to change is the n_grpclm_id depending on the query
Here's my current code:
Code:
Option Explicit
Public Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" Alias "DeleteUrlCacheEntryA" _
(ByVal lpszUrlName As String) As Long
Public Sub XMLhttp_Download_File()
Dim URL As String
Dim httpReq As xmlHTTP
Dim POSTdata As String
Dim HTMLdoc As HTMLDocument
Dim ViewStateInput As Object, EventValidationInput As Object
Dim ADODBstream As Object
Dim localFile As String
'The file name which the downloaded file will be saved as
localFile = "local file" 'local file here
URL = "[URL]http://URL[/URL] link.aspx" 'http url ending in .aspx
DeleteUrlCacheEntry URL
Set httpReq = New xmlHTTP
With httpReq
.Open "GET", URL, False
.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; .NET4.0C; .NET4.0E)"
.send
'Put response in a HTMLDocument
Set HTMLdoc = New HTMLDocument
HTMLdoc.body.innerHTML = .responseText
End With
'Construct the POST data string with the form parameters
POSTdata = "?processind=y&extractind=y&excellinkformat=y&getreserveind=A&getfininfo=y&n_clmgrp_id=6CFEC0A1C45BF6A2&n_clmgrp_id=79A1C19077AF9EFC&sourcesystem=all&submit=get+claims"
'Send POST data to request file
With httpReq
.Open "POST", URL, False
.setRequestHeader "Content-Type", "text/html; charset=utf-8"
.setRequestHeader "Content-Length", Len(POSTdata)
.setRequestHeader "Cache-Control", "private"
.send POSTdata
'Save response in local file
If .Status = 200 Then
Set ADODBstream = CreateObject("ADODB.Stream")
With ADODBstream
.Type = 1 'adTypeBinary
.Open
.write httpReq.responseBody
.SaveToFile localFile, 2
.Close
End With
End If
End With
End Sub
[CODE]