Excel VBA SendKeys not causing IE 9 to load

I am writing a macro that automates the process of downloading a csv file from my company's internal site. For many reasons, I cannot use any xmlhttp objects, and I got the macro until it loads the file. The problem is that when you click on the link to download the file, Internet Explorer offers the user the "Open", "Save" and "Cancel" buttons. Although in IE using alt + shift + s will save the load, but I cannot get the Send + "% + s" method from excel vba to work. Any help would be greatly appreciated. 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
+5
source share
4 answers

, " " .

webbrowser, URL- . , - Excel. " " VBA , .

. exe, VB6, IE Info. , API, .

, , exe vb6.

Excel .

. - URL-, URL-. , . , , . , , , . , .

enter image description here

C:\. , Shell .

Sub Sample()
    Dim sUrl As String

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

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

enter image description here

: .

+1

, IE 11:

  • C:\Windows\System32\UIAutomationCore.dll i.e C:\Users\admin\Documents, UIAutomationClient .
  • :

        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   
    

.

+1

, : IE9, , "" " ". "CTRL + J". , , "" "". , , , .

:

' 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}~"
0

Yours Application.Sendkeysjust needs to be set up. Below is the code I'm using, so it is tested on IE11. This is for Alt+ Swithout Shift, which is a keyboard shortcut in IE11. Let me know if this doesn't work and you need help adding Shiftback.

Application.SendKeys "%{S}", True

0
source

All Articles