Excel中VBA的SendKeys不会造成IE 9保存下载 [英] Excel VBA SendKeys not causing IE 9 to save download

查看:1295
本文介绍了Excel中VBA的SendKeys不会造成IE 9保存下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写,可自动下载从我公司的内部网站csv文件的过程中宏。对于很多原因,我不能使用任何XMLHTTP对象,我已经得到了宏观的地步,它会下载该文件。问题是,当点击链接来下载文件Internet Explorer提示与打开,保存用户,和取消按钮。而在IE浏览器,使用Alt + Shift + S将保存下载,但我似乎无法得到的SendKeys%+ S从Excel VBA的方法来工作。任何帮助将大大AP preciated。下面是相关code:

 功能followLinkByText(thetext作为字符串)作为布尔
   点击具有指定文本的第一个链接
    昏暗的ALINK为Variant    直到指定的文本中找到通过HTML文档中的每个锚环路
    '然后点击链接
    对于每个ALINK在ie.document.Links
       如果alink.innerHTML = thetext然后
            alink.Click
            waitForLoad
            Application.Wait现在+ TIMEVALUE(00:00:01)
            Application.SendKeys%+ S,真            followLinkByText = TRUE
            退出功能
        万一
     下一个结束功能


解决方案

就像我在我的评论中提到,在信息安全栏,很难与文件下载窗口交互。

另一种方法是使用WebBrowser控件,然后通过URL来了。但是,这种方法的主要问题是,你不能在同一个Excel实例的网页浏览器。一旦文件下载窗口弹出整个VBA宏将陷入停滞状态,直到时间,你不处理它。

下面是一个另类。下面是我在VB6这会弹出文件下载窗口绕过IE信息安全吧创建了一个小exe文件。一旦文件下载窗口弹出时,你可以用它使用的API互动如图我的<一个href=\"http://www.siddharthrout.com/2012/02/02/vbavb-netvb6click-opensavecancel-button-on-ie-download-window-part-ii/\"相对=nofollow>博客文章

让我们举个例子,看看我们如何与这个VB6的EXE文件进行互动。

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

重要提示:既然你没有给我任何URL,我采取​​了静态URL。请您的链接替换它。现在根据您指定的链接时,你可能会看到这两个下载Windows之一。根据你看你将不得不寻找基于如下所示的图片的窗口句柄下载窗口上。我给博客的链接了解详情。

下载附加的文件并将其保存在说 C:\\ 。如果你将它保存在其他任何位置,然后修改,在下面的Shell语句。

 子样品()
    昏暗SURL作为字符串    SURL =HTTP://s$p$padsheetpage.com/downloads/xl/king-james-bible.xlsm    壳牌C:\\ FDL.exe&放大器; SURL,vbNormalFocus
结束小组

快照

文件:文件可以下载这里

I am writing a Macro that automates the process of downloading a csv file from my company's internal website. For many reasons I can't use any xmlhttp objects, and I have gotten the macro to the point where it will download the file. The problem is that when it clicks the link to download the file Internet Explorer prompts the user with Open, Save, and Cancel buttons. While in IE, using alt+shift+s will save the download, but I can't seem to get the Sendkeys "%+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

解决方案

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.

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

FILE: The file can be downloaded here.

这篇关于Excel中VBA的SendKeys不会造成IE 9保存下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆