Is it possible to have VBA run this whole sequence and download the PDF? I have spent a lot of time researching, but can't find anything and I am not very knowledgeable with VBA. And help would be greatly appreciated. Thank you.
It is not a VBA concern (as there is nothing standard in VBA to perform this !) but a Web and Windows understanding !
Via VBA, you could use Windows API like URLDownloadToFile if you know each time the URL of the file to download.
See examples all over the Web and API documentation on MSDN website …
This saves the PDF in location - "C:\Users\Public\myPDF.pdf"
Note : This needs reference to "Microsoft Internet Controls". Goto "Tools" >> "References" and check the "Microsoft Internet Controls" option.
"I don't get things easily, so please be precise and elaborate"
If someone's post has helped you, thank by clicking on "Add Reputation" below the post.
If your query is resolved please mark the thread as "Solved" from the "Thread Tools" above.
First off, thank you so much for your help. I had to add "PtrSafe" to the declare function because I'm running 64 bit. So I'm getting 2 errors with the code. First:
I've tried researching what the problem may be, but no luck. I'll keep trying. If you have any idea what it may be, that'd be greatly appreciated. Thanks again!
So yeah, the code I was using that downloads via hyperlinks and is only working with documents I have already viewed. So cookie related. I've noticed that if you right-click and hit save link as, it saves the document. Is there any code to run that sequence? I can't find anything, but am determined. I'll look into that more once I figure out if I can get sourabhg's code to work.
Reading the href element wont help, as if you visit that href link directly, you will get and webpage wont load. We need to press the "11" button for that to work.
@skyscraper could you send me the statements where you are getting an error, for some reason I cant view your attachements.
Could you try running the code not by the button but my debugging it line by line (by pressing F8) in the module.
It works perfectly for me, I suspect that code is being triggered before the IE is ready. So, try running the code in debug mode line by line and let me know if that works.
As many samples from this forum or all over the Web …
Originally Posted by theskyscraper1
What href?
Because that gets an error.
You just forgot to add the URL base address ! (HTML basics …)
File can be directly downloaded from the "11" link and, as I wrote, without any webbrowser ‼
Neither any reference or Windows API, just by requests :
PHP Code:
Function RequestDownload$(URL$, ByVal FOLD$) Dim B() As Byte, F%, S$() With CreateObject("WinHttp.WinHttpRequest.5.1") .Open "GET", URL, False .SetRequestHeader "DNT", "1" On Error GoTo Fin .Send If .Status = 200 Then If Right(FOLD, 1) <> "\" Then FOLD = FOLD & "\" S = Split(.Option(1), "/") S(0) = FOLD & S(UBound(S)) B = .ResponseBody F = FreeFile(1) Open S(0) For Binary As #F Put #F, , B Close #F RequestDownload = S(0) End If Fin: End With End Function
Sub Demo1() Const ORI = "http://recorder.maricopa.gov/recdocdata/", _ URL = ORI & "GetRecDataDetail.aspx?rec=19930074944&bdt=1/1/1947&edt=11/18/2016", _ LNK = "<a id=""ctl00_ContentPlaceHolder1_lnkPages""" Dim SP$() With CreateObject("Msxml2.XMLHTTP") .Open "GET", URL, False .SetRequestHeader "DNT", "1" On Error Resume Next .Send On Error GoTo 0 If .Status <> 200 Then Beep: Debug.Print .Status & " : " & .StatusText: Exit Sub SP = Split(.ResponseText, LNK): If UBound(SP) < 1 Then Beep: Exit Sub End With SP = Split(Split(SP(1), "</a>")(0), "href="""): If UBound(SP) < 1 Then Beep: Exit Sub SP(0) = RequestDownload(ORI & Split(SP(1), """")(0), ThisWorkbook.Path) If SP(0) > "" Then MsgBox "File downloaded to " & vbLf & vbLf & SP(0), vbInformation Else Beep End Sub
Or …
PHP Code:
Sub Demo2() Const ORI = "http://recorder.maricopa.gov/recdocdata/", _ URL = ORI & "GetRecDataDetail.aspx?rec=19930074944&bdt=1/1/1947&edt=11/18/2016" Dim oDoc As Object, oElt As Object, S$ With CreateObject("Msxml2.XMLHTTP") .Open "GET", URL, False .SetRequestHeader "DNT", "1" On Error Resume Next .Send On Error GoTo 0 If .Status <> 200 Then Beep: Debug.Print .Status & " : " & .StatusText: Exit Sub Set oDoc = CreateObject("htmlfile") oDoc.Write .ResponseText End With Set oElt = oDoc.all("ctl00_ContentPlaceHolder1_lnkPages") If oElt Is Nothing Then Beep Else S = RequestDownload(Replace$(oElt.href, "about:", ORI), ThisWorkbook.Path) Set oElt = Nothing If S > "" Then MsgBox "File downloaded to " & vbLf & vbLf & S, vbInformation Else Beep End If Set oDoc = Nothing End Sub
Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !
Re: As many samples from this forum or all over the Web …
Hi Marc,
Thank you for the code, but I get the beep with Demo 1 and Demo 2. And nothing happens.
Are there any settings I need to change?
So it finds "ctl00_ContentPlaceHolder1_lnkPages" and then gets the following URL? I'm trying to figure this out, but I'm a novice. If you can help me, that'd be greatly appreciated, otherwise I'm going to have to go back to the only way I know that works using IE. Thanks again for your help.
Last edited by theskyscraper1; 11-20-2016 at 02:46 PM.
Bookmarks