+ Reply to Thread
Results 1 to 19 of 19

VBA Download PDF

  1. #1
    Registered User
    Join Date
    11-18-2016
    Location
    Seattle, Washington
    MS-Off Ver
    2013
    Posts
    60

    VBA Download PDF

    Is it possible to download a PDF from the link on this page? When you click the page count (11) on this webpage:

    http://recorder.maricopa.gov/recdocd...3=&doc4=&doc5=

    it runs a sequence of events and produces a PDF. When you input just the hyperlink:

    http://recorder.maricopa.gov/recdocd...Documents&suf=

    After this link, it goes to:

    http://156.42.40.50/UnOfficialDocs2/pdf/19930074944.pdf

    And opens the PDF.

    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.

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: VBA Download PDF


    Hi !

    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 …

  3. #3
    Forum Expert sourabhg98's Avatar
    Join Date
    10-22-2014
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007, 2013
    Posts
    1,899

    Re: VBA Download PDF

    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.

    Please Login or Register  to view this content.
    Attachment :- excelforum006.xlsm
    Happy to Help

    How to upload excel workbooks at this forum - http://www.excelforum.com/the-water-...his-forum.html

    "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.

    Sourabh

  4. #4
    Registered User
    Join Date
    11-18-2016
    Location
    Seattle, Washington
    MS-Off Ver
    2013
    Posts
    60

    Re: VBA Download PDF

    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:

    Attachment 490020

    When I debug, it shows:

    Attachment 490021

    The second error, which is likely related, is:

    Attachment 490022

    Debugging shows:

    Attachment 490023

    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!

  5. #5
    Registered User
    Join Date
    11-18-2016
    Location
    Seattle, Washington
    MS-Off Ver
    2013
    Posts
    60

    Re: VBA Download PDF

    Accident. Please ignore.

  6. #6
    Registered User
    Join Date
    11-18-2016
    Location
    Seattle, Washington
    MS-Off Ver
    2013
    Posts
    60

    Re: VBA Download PDF

    Marc L,

    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.

  7. #7
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: VBA Download PDF


    You can read the link of an element in the href property of its fisrt children …

  8. #8
    Forum Expert sourabhg98's Avatar
    Join Date
    10-22-2014
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007, 2013
    Posts
    1,899
    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.

    Regards
    Sourabh

  9. #9
    Registered User
    Join Date
    11-18-2016
    Location
    Seattle, Washington
    MS-Off Ver
    2013
    Posts
    60

    Re: VBA Download PDF

    Yeah, sorry that didn't work. So the first error is a

    Run-time error '13': Type mismatch

    http://i.imgur.com/1d9JTqt.jpg

    The debugger shows:

    Set obj1 = IE.document.getElementByID("ct100_ContentPlaceHolder1_LnkPages") as the error

    The second error is:

    Run-time error '91': Object variable or with block variable not set

    The debugger shows:

    IE-Tab.Quit as the error

    You're an awesome person for trying to help me. Thank you very much. I'm determined to find a way around this. Thanks!

  10. #10
    Registered User
    Join Date
    11-18-2016
    Location
    Seattle, Washington
    MS-Off Ver
    2013
    Posts
    60

    Re: VBA Download PDF

    Yeah, sorry that didn't work. So the first error is a

    Run-time error '13': Type mismatch

    The debugger shows:

    Set obj1 = IE.document.getElementByID("ct100_ContentPlaceHolder1_LnkPages") as the error

    http://i.imgur.com/6xw72Ez.jpg

    The second error is:

    Run-time error '91': Object variable or with block variable not set

    The debugger shows:

    IE-Tab.Quit as the error

    http://i.imgur.com/gzu9hqb.jpg

    You're an awesome person for trying to help me. Thank you very much. I'm determined to find a way around this. Thanks!

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow


    So from the href property of the page link (11) with its ID the PDF URL can be directly constructed
    and without piloting IE but just via a request …

  12. #12
    Registered User
    Join Date
    11-18-2016
    Location
    Seattle, Washington
    MS-Off Ver
    2013
    Posts
    60

    Re: VBA Download PDF

    Marc,

    What href? This:

    http://recorder.maricopa.gov/recdocd...Documents&suf=

    Because that gets an error.

  13. #13
    Forum Expert sourabhg98's Avatar
    Join Date
    10-22-2014
    Location
    New Delhi, India
    MS-Off Ver
    Excel 2007, 2013
    Posts
    1,899
    @skyscraper

    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.

  14. #14
    Registered User
    Join Date
    11-18-2016
    Location
    Seattle, Washington
    MS-Off Ver
    2013
    Posts
    60

    Re: VBA Download PDF

    Thanks Soura,

    I tried that and it loops in:

    For Each ele In IE.document.getelementsbytagname("a")
    If InStr(ele.innertext, "11") > 0 Then
    ele.Click
    IE.Visible = False
    Exit For
    End If

    Next

    It looks like this code is specific to page 11, is that right? Some documents will have different page numbers. Thanks.

  15. #15
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool As many samples from this forum or all over the Web …

    Quote Originally Posted by theskyscraper1 View Post
    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 ByteF%, S$()
        
    With CreateObject("WinHttp.WinHttpRequest.5.1")
            .
    Open "GET"URLFalse
            
    .SetRequestHeader "DNT""1"
            
    On Error GoTo Fin
            
    .Send
         
    If .Status 200 Then
             
    If Right(FOLD1) <> "\" 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"URLFalse
            
    .SetRequestHeader "DNT""1"
            
    On Error Resume Next
            
    .Send
            On Error 
    GoTo 0
            
    If .Status <> 200 Then BeepDebug.Print .Status " : " & .StatusText: Exit Sub
            SP 
    Split(.ResponseTextLNK):  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 ObjectoElt As ObjectS$
        
    With CreateObject("Msxml2.XMLHTTP")
            .
    Open "GET"URLFalse
            
    .SetRequestHeader "DNT""1"
            
    On Error Resume Next
            
    .Send
            On Error 
    GoTo 0
            
    If .Status <> 200 Then BeepDebug.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
            
    RequestDownload(Replace$(oElt.href"about:"ORI), ThisWorkbook.Path)
            
    Set oElt Nothing
            
    If "" Then MsgBox "File downloaded to " vbLf vbLf SvbInformation Else Beep
        End 
    If
            
    Set oDoc Nothing
    End Sub 
    Do you like it ? So thanks to click on bottom left star icon « Add Reputation » !

  16. #16
    Registered User
    Join Date
    11-18-2016
    Location
    Seattle, Washington
    MS-Off Ver
    2013
    Posts
    60

    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.

  17. #17
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: VBA Download PDF


    I do not know as on my side with different Windows version both procedures well download file without any issue …

    Try to progress in code in step by step mode to see where the beep occurs …

  18. #18
    Registered User
    Join Date
    11-18-2016
    Location
    Seattle, Washington
    MS-Off Ver
    2013
    Posts
    60

    Re: VBA Download PDF

    Thanks for the response! I've done that already and Demo 1 Ends after passing through:

    SP(0) = RequestDownload(ORI & Split(SP(1), """")(0), ThisWorkbook.Path)

    It doesn't go to:

    If SP(0) > "" Then MsgBox "File downloaded to " & vbLf & vbLf & SP(0), vbInformation Else Beep
    End Sub

    Demo 2 ends after passing through:

    S = RequestDownload(Replace$(oElt.href, "about:", ORI), ThisWorkbook.Path)

    It doesn't go to:

    Set oElt = Nothing
    If S > "" Then MsgBox "File downloaded to " & vbLf & vbLf & S, vbInformation Else Beep
    End If
    Set oDoc = Nothing
    End Sub

    Could you attach your work? I'd like to see how it's supposed to end/work? This would be way better than the IE option. Thanks again!

  19. #19
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: VBA Download PDF


    All code is located in a standard module, nothing more, no reference …

    You want the pdf file result as an attachment ?! It is the same if you manually download it …

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. how can i download user details from mysql on date basis
    By baig123 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-19-2014, 07:31 AM
  2. Download XML through VBA
    By MBD in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-08-2014, 12:18 AM
  3. Can't download from web
    By plato in forum Excel General
    Replies: 8
    Last Post: 01-18-2010, 06:32 AM
  4. [SOLVED] Useful add-ins to download
    By [email protected] in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-17-2006, 03:20 PM
  5. [SOLVED] Useful add-ins to download
    By [email protected] in forum Excel General
    Replies: 0
    Last Post: 03-17-2006, 12:25 AM
  6. [SOLVED] Download from SAP too big
    By Les Stout in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-13-2005, 04:05 PM
  7. Download .png
    By spaceman33 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-02-2005, 01:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1