5

I am writing a macro to download a csv file from my company's internal website.

For many reasons I can't use any xmlhttp objects. The macro will download the file. The problem is Internet Explorer 9 prompts the user with Open, Save, and Cancel buttons.

While in IE, Alt+Shift+S will save the download, but I can't get the Sendkeys "%+s" method from Excel VBA to work.

Here is the relevant code:

Function followLinkByText(thetext As String) As Boolean
   'clicks the first link that has the specified text
    Dim alink As Variant

    'Loops through every anchor in HTML document until specified text is found
    ' then clicks the link
    For Each alink In ie.document.Links
       If alink.innerHTML = thetext Then
            alink.Click
            'waitForLoad
            Application.Wait Now + TimeValue("00:00:01")
            Application.SendKeys "%+s", True

            followLinkByText = True
            Exit Function
        End If
     Next

End Function
karel
  • 5,489
  • 46
  • 45
  • 50
derigible
  • 964
  • 5
  • 15
  • 32
  • If I am not wrong then the only place on the web which extensively covers on how to interact with the IE File Download is here... http://siddharthrout.wordpress.com/2012/02/02/vbavb-netvb6click-opensavecancel-button-on-ie-download-window-part-ii/ – Siddharth Rout Jul 24 '12 at 20:50
  • If you have a direct link to the file it's probably easier using the URLDownloadToFile API function instead. There's one of many examples here http://www.cpearson.com/excel/DownloadFile.aspx –  Jul 24 '12 at 21:38
  • @osknows: I doubt if it is a direct link. BTW, I have covered `URLDownloadToFile API function` in Part 1 of the above link as well. – Siddharth Rout Jul 24 '12 at 21:41
  • It is not a direct link. It is an image that calls a javascript function. I looked into trying to call the javascript directly but the page requires an id that is not given until the user clicks on a certain link that then pulls the id from a database and puts it on the page using ajax. I was hoping to avoid having to understand the javascript since i am not familiar with javascript in general. @Rout Does IE9 have the same SendMessage limitations as previous IE versions? IE9 doesn't create a new window but rather a popup at the bottom of the screen. Would this method work in the same fashion? – derigible Jul 24 '12 at 21:52
  • @derigible: What you refer to is called an info bar. AFAIK, There is no way you an bypass that no matter what you read on the web. I got around that by passing the url to a VB6 exe using shell. Fortunately the info bar doesn't work when the URL is called via a webbrowser. I have in fact given many solution to my clients based on that. I can pass on that exe to you as well. All you have to do is shell it from VBA and the File Download will automatically pop up with which you can interact using the API. CONTD... – Siddharth Rout Jul 24 '12 at 22:41
  • An alternative (I haven't tested it) would be to launch a different instance of Excel and then using the webbrowser launch the URL and it will then use the same principle that I mentioned on my blog once the File Download window appears. – Siddharth Rout Jul 24 '12 at 22:41
  • If you can share the URL, I can try to give you the exact example. – Siddharth Rout Jul 24 '12 at 22:44
  • I am unfamiliar with what VB6 exe. If you can send me a link to this and an explanation on how it works I would be most appreciative. Thank you for your help! – derigible Jul 24 '12 at 22:52

4 Answers4

1

Like I mentioned in my comments, The Info Security bar makes it difficult to interact with the File Download Window.

An alternative is to use the webbrowser control and then passing the URL to it. But the main problem with this method is that you cannot have the webbrowser in the same Excel Instance. Once the File Download window pops up your entire VBA Macro will come to a standstill till the time you do not dispose it off.

Here is an alternative. Here is a small exe that I created in VB6 which will pop up the File Download window bypassing the IE Info Security Bar. And once the File Download window pops up, you can interact with it using the APIs as shown in my blog article.

Let's take an example to see on how we interact with this vb6 exe file.

Create a module in Excel and paste this code.

IMPORTANT NOTE: Since you didn't give me any URL, I am taking a Static URL. Please replace it with your link. Now depending upon the link that you specify, you might see the one of these two download windows. Based on the download window that you see you will have to find the window handles based on the pic shown below. More details on the blog link that I gave.

enter image description here

Download the file attached and save it in say C:\. If you save it in any other location then amend that in the Shell statement below.

Sub Sample()
    Dim sUrl As String

    sUrl = "http://spreadsheetpage.com/downloads/xl/king-james-bible.xlsm"

    Shell "C:\FDL.exe " & sUrl, vbNormalFocus
End Sub

SNAPSHOT

enter image description here

FILE: The file can be downloaded here.

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • NOTE: If your url needs a current session to be active then let me know, I will upload a different exe. But for that you will have to give me the Base URL Path. – Siddharth Rout Jul 25 '12 at 09:14
  • This answer works great! I appreciate the help you gave to me. This has saved me a great amount of effort. I found that for some reason my sendKeys was not working at all for IE 9, so I have posted a question regarding that problem, but this solution fixed this problem. – derigible Jul 25 '12 at 16:55
  • For anyone interested, that new question is [here](http://stackoverflow.com/q/11655591/698590). – Gaffi Jul 25 '12 at 18:28
  • one thing Siddharth, what if the info "open save cancel" prompt is generated by clicking a button that creates the file which brings up this prompt (ie. you don't have a source location because it is generated from the button which pops up the info bar) –  Dec 15 '12 at 01:49
1

You may try this as it is worked for me on IE 11:

  1. Copy file C:\Windows\System32\UIAutomationCore.dll file to users Documents i.e C:\Users\admin\Documents then add reference UIAutomationClient to your macro file.
  2. Paste below code in your module:

        Option Explicit
        Dim ie As InternetExplorer
        Dim h As LongPtr
        Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr
    
    Sub Download()
        Dim o As IUIAutomation
        Dim e As IUIAutomationElement
        Set o = New CUIAutomation
        h = ie.Hwnd
        h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)
        If h = 0 Then Exit Sub
    
        Set e = o.ElementFromHandle(ByVal h)
        Dim iCnd As IUIAutomationCondition
        Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Save")
    
        Dim Button As IUIAutomationElement
        Set Button = e.FindFirst(TreeScope_Subtree, iCnd)
        Dim InvokePattern As IUIAutomationInvokePattern
        Set InvokePattern = Button.GetCurrentPattern(UIA_InvokePatternId)
        InvokePattern.Invoke
    End Sub   
    

Try at your end.

Lifewithsun
  • 968
  • 14
  • 34
  • Hi It worked well for me. But I need to save as the file. If I change it to "Save as" instead of "Save", it is throwing error. Suggest me how to enter into save-As dialog box, by clicking Save-As button in the ribbon. – Abi Aug 11 '15 at 10:47
0

I think I came up with a simpler solution: when the download bar appears in IE9, just by-pass it by displaying the "real" Download Pop Up window. The shortcut is "CTRL+J". All you have to do next is click on "Save" or "Open". There might be pretty ways to do it, but I simply send a key sequence to move the focus on desired option and then press enter.

Here is the code:

' Wait for download bar to appear
Application.Wait (Now + TimeValue("0:00:04"))

' Sending CTRL+J to open download pop-up
SendKeys "^j"

' Wait for download popup to appear
Application.Wait (Now + TimeValue("0:00:02"))

' Sending keys sequence to click on "Save" button
SendKeys "{RIGHT}{RIGHT}{RIGHT}~"
nobody
  • 19,814
  • 17
  • 56
  • 77
Rodolphe
  • 1
  • 1
0

Your Application.Sendkeys just needs a tweak. Below is the code I am using so it is tested on IE11. This is for Alt+S with no Shift which is the keyboard shortcut in IE11. Let me know if this doesn't work and you need help adding the Shift back in.

Application.SendKeys "%{S}", True

Tony L.
  • 17,638
  • 8
  • 69
  • 66