We receive emails with links to documents, or more precisely, links to webpages that contain buttons we click to download documents. I want to automate this under Office 2016, IE 11, and Windows 7 Enterprise. I've gotten this far.
In VBA, I set references to Microsoft HTML Object and Internet Controls libraries. I load the page, locate the button control on the page, and issue the button's click method. Then I get the prompt you see above. I want to save as... that is, each downloaded file will go in a particular directory. How would you do that?
EDIT: The URL to the file is not contained in the web page's source. The code for the Download button is this:
<form action="/Home/NoCaptcha" method="post">
<input type="submit" class="btn btn-primary btn-lg" value="Download" style="margin-top: 40px;" />
</form>
The prompt is not part of the webpage, so I can't use HTML to find the button. I could use SendKeys (not ideal), but even then, I could only send Alt-S to save. Is there a way of virtually clicking on the down arrow to expose the Save As option? But if I do this, how do I enter the location of the file? Would I have to somehow control the actual Save As dialog box?
Assuming I can use SendKeys to send an Alt-S, just before doing that, I could change the registry key that holds the default download location. But this would involve writing to the registry hundreds of times per day. Are there consequences to this? Do I risk corrupting the registry?
EDIT: I suppose if IE automatically downloaded the file, that would solve my problem too Further research uncovers that the option to download without prompts was removed since IE 10.
Since you have the URL of the page you have just about everything you need to get the file, Part of the post has a version param that does not seem to be included in the original URL. So in short parse out the download button url to get the DocumentLocator ID and the PublicKey. You can also parse through the HTML which has all the info, DocLocID, Version and Key as well.
this is the html source of the form in https://eservices.truecertify.com/?loc=1DC-IAJJJ4-AE58564C&key=Asbg
this is where you get the data to assemble the final URL string
https://eservices.truecertify.com/Home/NoCaptcha?BypassCaptcha=True&DocumentLocator=1DC-IAJJJ4-AE58564C&PublicKey=Asbg&VersionNumber=Version+3.0.0.3
<form action="/Home/NoCaptcha" method="post">
<input type="submit" class="btn btn-primary btn-lg" value="Download" style="margin-top: 40px;">
<input data-val="true" data-val-required="The BypassCaptcha field is required." id="BypassCaptcha" name="BypassCaptcha" type="hidden" value="True">
<input data-val="true" data-val-required="The DocumentLocator field is required." id="DocumentLocator" name="DocumentLocator" type="hidden" value="1DC-IAJJJ4-AE58564C">
<input data-val="true" data-val-required="The PublicKey field is required." id="PublicKey" name="PublicKey" type="hidden" value="Asbg">
<input id="VersionNumber" name="VersionNumber" type="hidden" value="Version 3.0.0.3">
</form>
.
Sub TestMe()
'URL of download button
'https://eservices.truecertify.com/?loc=1DC-IAJJJ4-AE58564C&key=Asbg
'within the loc is the document locator ID, the key is the public key, and you will need to check to see if the version changes
'the Posted URL
'BypassCaptcha=True&DocumentLocator=1DC-IAJJJ4-AE58564C&PublicKey=Asbg&VersionNumber=Version+3.0.0.3
Dim sURL As String
sURL = "https://eservices.truecertify.com/Home/NoCaptcha?BypassCaptcha=True&DocumentLocator=1DC-IAJJJ4-AE58564C&PublicKey=Asbg&VersionNumber=Version+3.0.0.3"
SavePDFFile sURL
End Sub
Sub SavePDFFile(myURL As String)
Dim savePath As String
Dim WinHttpReq As Object
savePath = "C:\"
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
'I used get instead of post
WinHttpReq.Open "GET", myURL, False
WinHttpReq.send
If WinHttpReq.Status = 200 Then
Set oStream = CreateObject("ADODB.Stream")
oStream.Open
oStream.Type = 1
oStream.Write WinHttpReq.responseBody
oStream.SaveToFile (savePath & "MyFile.pdf")
oStream.Close
End If
End Sub