5

我正在编写一个宏来从我公司的内部网站下载一个 csv 文件。

由于许多原因,我不能使用任何 xmlhttp 对象。宏将下载文件。问题是 Internet Explorer 9 使用打开、保存和取消按钮提示用户。

在 IE 中,Alt+Shift+S 将保存下载,但我无法从 Excel VBA 中获取 Sendkeys "%+s" 方法。

以下是相关代码:

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
4

4 回答 4

1

就像我在评论中提到的那样,信息安全栏使与文件下载窗口的交互变得困难。

另一种方法是使用 webbrowser 控件,然后将 URL 传递给它。但是这种方法的主要问题是您不能在同一个 Excel 实例中拥有网络浏览器。一旦文件下载窗口弹出,您的整个 VBA 宏将停止,直到您不将其处理掉。

这是一个替代方案。这是我在 VB6 中创建的一个小 exe,它将绕过 IE 信息安全栏弹出文件下载窗口。一旦弹出“文件下载”窗口,您就可以使用我的博客文章中所示的 API 与之交互。

让我们举个例子来看看我们如何与这个 vb6 exe 文件进行交互。

在 Excel 中创建一个模块并粘贴此代码。

重要提示:由于您没有给我任何 URL,我正在使用静态 URL。请用您的链接替换它。现在,根据您指定的链接,您可能会看到这两个下载窗口之一。根据您看到的下载窗口,您必须根据下图所示找到窗口句柄。有关我提供的博客链接的更多详细信息。

在此处输入图像描述

下载附件并保存在 say 中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

快照

在此处输入图像描述

文件:文件可以在这里下载。

于 2012-07-24T23:37:08.450 回答
1

你可以试试这个,因为它在 IE 11 上对我有用:

  1. 将文件C:\Windows\System32\UIAutomationCore.dll文件复制到用户文档,即C:\Users\admin\Documents添加UIAutomationClient对您的宏文件的引用。
  2. 将以下代码粘贴到您的模块中:

        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   
    

在你的最后尝试。

于 2014-11-04T14:42:07.037 回答
0

我想我想出了一个更简单的解决方案:当下载栏出现在 IE9 中时,只需通过显示“真正的”下载弹出窗口来绕过它。快捷方式是“CTRL+J”。您接下来要做的就是单击“保存”或“打开”。可能有很多方法可以做到这一点,但我只需发送一个按键序列以将焦点移到所需选项上,然后按 Enter。

这是代码:

' 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}~"
于 2014-07-16T17:33:17.107 回答
0

Application.Sendkeys只需要调整一下。以下是我正在使用的代码,因此在 IE11 上进行了测试。这是IE11 中的键盘快捷键,用于Alt+S没有。Shift如果这不起作用,请告诉我,您需要帮助添加Shift回。

Application.SendKeys "%{S}", True

于 2016-10-12T22:24:50.663 回答