VBA Internet Explorer 自动化 - 如何在下载文件时选择“打开”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10400795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Internet Explorer Automation - How to Select "Open" When Downloading a File
提问by Lane Sawyer
This is my first question ever here on stackoverflow!
这是我在 stackoverflow 上的第一个问题!
I've been searching for a solution to this problem for a while and haven't found any help. I may just be using the wrong keywords in my searches, but so far I've had no luck. Here's the question:
我一直在寻找这个问题的解决方案,但没有找到任何帮助。我可能只是在搜索中使用了错误的关键字,但到目前为止我没有运气。这是问题:
In VBA, how can I select the "Open" option from the file download dialog in Internet Explorer?
在 VBA 中,如何从 Internet Explorer 的文件下载对话框中选择“打开”选项?
Just for extra clarification, I'm talking about the yellow-orange bar that pops up across the bottom of the screen in IE9 when a file is downloaded.
只是为了额外说明,我说的是下载文件时在 IE9 屏幕底部弹出的黄橙色条。
I'm doing some VBA automation to download hundreds of PDFs from the web using Internet Explorer, but there is an intermediate step where a .fdf file has to be opened before I get to the actual PDF. So I first need to select the "Open" option so that I can move on to the next step of the automation. Like I said earlier, I've done a lot of searching and had no luck so far.
我正在做一些 VBA 自动化,以使用 Internet Explorer 从网络下载数百个 PDF,但有一个中间步骤,在我获得实际的 PDF 之前,必须打开 .fdf 文件。所以我首先需要选择“打开”选项,以便我可以继续下一步的自动化。就像我之前说的,我已经做了很多搜索,但到目前为止还没有运气。
I've tried using SendKeys
in hopes that hitting Enterwould work, and that was a last ditch effort that didn't work.
我曾尝试使用SendKeys
,希望击球Enter能奏效,但这是最后一次没有奏效的努力。
Thanks in advance for the help!
在此先感谢您的帮助!
采纳答案by Siddharth Rout
I have covered this extensively here.
我在这里广泛地介绍了这一点。
Topic: VBA/VB.Net/VB6–Click Open/Save/Cancel Button on IE Download window – PART I
主题:VBA/VB.Net/VB6——在IE下载窗口点击打开/保存/取消按钮——第一部分
链接:http: //www.siddharthrout.com/2011/10/23/vbavb-netvb6click-opensavecancel-button-on-ie-download-window/
and
和
EDIT (IMP) If you are using IE 9 Do not forget to read PART 2 as it includes and covers the window structure of IE 9 download window
编辑 (IMP) 如果您使用的是 IE 9 不要忘记阅读第 2 部分,因为它包括并涵盖了 IE 9 下载窗口的窗口结构
Topic: VBA/VB.Net/VB6–Click Open/Save/Cancel Button on IE Download window – PART II
主题:VBA/VB.Net/VB6——在IE下载窗口点击打开/保存/取消按钮——第二部分
链接:http: //www.siddharthrout.com/2012/02/02/vbavb-netvb6click-opensavecancel-button-on-ie-download-window-part-ii/
The above links discuss on how to use use the API's to achieve what you want.
上面的链接讨论了如何使用 API 来实现您想要的。
From the 1st link...
从第一个链接...
Like you and me, we both have names, similarly windows have “handles” (hWnd), Class etc. Once you know what that hWnd is, it is easier to interact with that window.
Findwindow API finds the hWnd of a particular window by using the class name and the caption of the window (“File Download”) in this case. The “Open“, “Save” and “Cancel” buttons are windows in itself but they are child windows of the main window which is “File Download“. That means each one of those will also have a hWnd :) To find the child windows, we don't use FindWindow but use FindWindowEx. All the three buttons “Open“, “Save” and “Cancel” have the same class which is “ Button”.
就像你我一样,我们都有名字,类似的窗口有“句柄”(hWnd)、类等。一旦你知道 hWnd 是什么,与那个窗口交互就更容易了。
在这种情况下,Findwindow API 通过使用类名和窗口标题(“文件下载”)来查找特定窗口的 hWnd。“打开”、“保存”和“取消”按钮本身是窗口,但它们是“文件下载”主窗口的子窗口。这意味着每一个也将有一个 hWnd :) 为了找到子窗口,我们不使用 FindWindow,而是使用 FindWindowEx。“打开”、“保存”和“取消”这三个按钮都具有相同的类,即“按钮”。
回答by user2444791
Similar post: link
类似帖子:链接
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, "Open")
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
回答by Tony L.
I sent the shortcut keys to the application. Here they are for IE11. Sorry I could not test in IE9. If you hold down Alt, it may show you the other key to the combo as IE11 does.
我将快捷键发送到应用程序。在这里,它们适用于 IE11。抱歉,我无法在 IE9 中进行测试。如果您按住Alt,它可能会像 IE11 一样显示组合键的另一个键。
Note: the code will not run as you expect if IE is not the active window on your machine so it won't work while in debug mode.
注意:如果 IE 不是您机器上的活动窗口,代码将不会按您的预期运行,因此它在调试模式下将无法运行。
- Shortcut key:Alt+O
- VBA:
Application.SendKeys "%{O}"
- 快捷键:Alt+O
- VBA:
Application.SendKeys "%{O}"