1

I Can able to access the website and click on Download button, once I clicked it download dialog box opens at the bottom with buttons Open,Save,Cancel. I would like to click on Open button and print the opened file in Cute pdf format.

How do i click the Open button Please help me out.

Screen shot of dialog box

Code used to click download button

ie2.Document.forms("ViewReferral").getElementsByClassName("notsuccess")(0).getElementsByTagName("tr").Item(1).getElementsByTagName("td").Item(3).getElementsByTagName("A").Item(0).Click

Once I clicked it above mentioned Dialog box opens.

Thank You.

braX
  • 11,506
  • 5
  • 20
  • 33
arun v
  • 852
  • 7
  • 19
  • 1
    that button is not part of the web page. Have you tried researching how to click IE command buttons? Also, will the flename always be the same? There are easier ways to download a file. – ashleedawg Mar 09 '18 at 16:24
  • Yes, you are correct its not part of web page. I did some research and tried to download it directly without clicking the download button using "href" in website but it fails. – arun v Mar 09 '18 at 16:29
  • so you can retrieve the complete url for the file? For example: if you retrieve it manually, and paste it into the browser's address bar, does the file download? – ashleedawg Mar 09 '18 at 16:36
  • I cant get the complete URL because once i clicked the button it will not navigating to another browser href= ../common/documentation.php?Action=Print&DocumentationId=XXXX&origin=viewreferral.php – arun v Mar 09 '18 at 16:37
  • Also I tried using Send keys method but it not reliable all the time, so looking for an better way to accessing it. – arun v Mar 09 '18 at 16:39
  • I don't understand. If you manually paste the url into the browser's address bar, does the file download? Example: `http://ipv4.download.thinkbroadband.com/5MB.zip` this begins downloading instantly. Does your URL do the same? The URL for the FILE not the page. – ashleedawg Mar 09 '18 at 16:39
  • No? its not happening. – arun v Mar 09 '18 at 16:46
  • what do you mean it's not happening? If you click [here](http://ipv4.download.thinkbroadband.com/5MB.zip) a sample file begins downloading automatically, does it not? Perhaps my browser is setup differently than yours. – ashleedawg Mar 09 '18 at 16:47
  • If I manually paste the url into the browser's address bar, file not get downloaded – arun v Mar 09 '18 at 16:48
  • ok - well do you have the complete url for the file? (`http://......attached_doc135155.pdf`) – ashleedawg Mar 09 '18 at 16:49
  • No @ashleedawg i don't have the complete url for the file, just having the connection string with the website server. – arun v Mar 09 '18 at 16:51
  • oh. Well disregard my answer then; I was trying to find you an alternative, assuming you were able to find the complete url in the page's source. – ashleedawg Mar 09 '18 at 17:02
  • You can find code for "Open" or "Save" in [both](https://stackoverflow.com/questions/48787790/when-using-vba-to-open-a-csv-file-directly-from-internet-explorer-i-cant-then-i/48788548#48788548) of [these](https://stackoverflow.com/questions/48560702/vba-ie-automation-wait-for-the-download-to-complete/48561571#48561571) questions, or as the answer to [this one](https://stackoverflow.com/a/40009054/9290986) – Chronocidal Mar 09 '18 at 17:03
  • @Chronocidal Thank you so much but I don't want to use send Keys method. – arun v Mar 09 '18 at 17:09
  • @arunv Then use the IUIAutomationCondition method in Question part of the second link? – Chronocidal Mar 09 '18 at 17:11
  • 1
    @arunv Perhaps [this](https://stackoverflow.com/a/32152712/8112776) will help – ashleedawg Mar 09 '18 at 17:12

1 Answers1

1

Instead of trying to click the button on the browsers' "Open/Save/Cancel" bar, you can download the file directly as long as you're able to retrieve the complete URL (ie., http://......attached_doc135155.pdf) through whatever method of scraping.


Download a file from a URL using VBA

Option Explicit

Sub downloadFile(url As String, filePath As String)

    Dim WinHttpReq As Object, attempts As Integer, oStream
    attempts = 3
    On Error GoTo TryAgain
TryAgain:
    attempts = attempts - 1
    Err.Clear
    If attempts > 0 Then
        Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
        WinHttpReq.Open "GET", url, False
        WinHttpReq.send

        If WinHttpReq.Status = 200 Then
            Set oStream = CreateObject("ADODB.Stream")
            oStream.Open
            oStream.Type = 1
            oStream.Write WinHttpReq.responseBody
            oStream.SaveToFile filePath, 2 ' 1 = no overwrite, 2 = overwrite
            oStream.Close
            MsgBox "File downloaded to:" & vbLf & filePath
        End If
    Else
        MsgBox "Failed."
    End If

End Sub

Sub testDownload()
    Const testFileURL = "http://ipv4.download.thinkbroadband.com/5MB.zip"
    Const localSavePathFile = "c:\5MB_testfile.zip"
    downloadFile testFileURL, localSavePathFile
End Sub

(Source)

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
  • Thank you so much for your time @ashleedawg, its not working since i don't have an complete url. – arun v Mar 09 '18 at 17:08