VBA/IE How to get past file save prompt?

后端 未结 1 695
暗喜
暗喜 2021-01-07 08:51

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 201

相关标签:
1条回答
  • 2021-01-07 09:38

    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
    
    0 讨论(0)
提交回复
热议问题