-1

I'm downloading an Excel file from particular website, but IE11 promoting me to click save button and I want it to automate using VBA.

I've tried Application.SendKeys("%s") method but it didn't work since I'm not expert in VBA I can't go further.

Sub dailyreport()
    Dim IE As Object    
    Dim doc As HTMLDocument
    Dim Element As HTMLLinkElement

    Set IE = New InternetExplorer
    IE.Visible = True
    IE.navigate ("https://mywebsite.com")

    Do While IE.Busy
        Application.Wait DateAdd("S", 1, Now)
    Loop

    Set doc = IE.document
    doc.getElementById("login").Value = "username"
    doc.getElementById("password").Value = "password"
    doc.getElementById("loginButton").Click

    ' Navigate to report page Do While IE.Busy Application.Wait DateAdd ("S", 1, Now) Loop IE.navigate ("https://mywebsite.com/report") 'Since the hyperlink doesn' t have any link I 'm searching the link with href value (Export View)

    Do While IE.Busy
        Application.Wait DateAdd("S", 30, Now)
    Loop

    For Each Element In doc.Links
        If InStr(Element.innerText, "Export View") Then
            Call Element.Click
            Exit For
        End If
    Next Element
End Sub

After clicking the Export View hyperlink it's starting to download but at the end IE asking me to click open or save button.

"Do you want to save or open"

So I wanted to click save button using VBA.

Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • Please format your code. adding three backtick/backquote(**`**) before and after the code will help you do that. Please check this out -> [How do I format my code block](https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks) – Amit Yadav Oct 20 '19 at 16:37
  • Possible duplicate of [Controlling IE11 “Do you want to Open/Save” dialogue window buttons in VBA](https://stackoverflow.com/q/32145437/11683) – GSerg Oct 20 '19 at 18:47
  • I've tried Application.SendKeys but I'm not sure how to handle it.... – Chakaravarthi N Oct 21 '19 at 01:42
  • This above code works fine up to clicking the hyperlink (export view) and all I need to know how click SAVE button after it download it from that website. – Chakaravarthi N Oct 21 '19 at 05:09

1 Answers1

0

Please refer to the following code, before click the download prompt Save button, we could wait the prompt appear, then using the Application.SendKeys "%{s}" command to click the Save button:

Sub Test()
    Dim IE As Object

    Dim startDateText As Object, endDateText As Object

    Set IE = CreateObject("InternetExplorer.Application")
    With IE
        .Visible = True
        .Navigate "<the website url>"

        While IE.ReadyState <> 4
            DoEvents
        Wend

        'click the button to download the file.
        IE.Document.getElementbyId("btnDowloadReport").Click

        'wait the download prompt appear
        Application.Wait (Now + TimeValue("00:00:03"))

        Application.SendKeys "%{s}"

        'Waiting for the site to load.
    End With
    Set IE = Nothing
End Sub

The web page content:

<a id="btnDowloadReport" href="https://research.google.com/pubs/archive/44678.pdf" download>Download</a>
Zhi Lv
  • 18,845
  • 1
  • 19
  • 30
  • For Each Element In doc.Links If InStr(Element.innerText, "Export View") Then Call Element.Click Exit For End If Next Element Do While IE.Busy Application.Wait DateAdd("S", 180, Now) Loop Application.Wait (Now + TimeValue("00:00:08")) Application.SendKeys "%{s}" End Sub – Chakaravarthi N Oct 31 '19 at 15:02