在 VBA 中控制 IE11“你想打开/保存”对话窗口按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32145437/
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
Controlling IE11 "Do you want to Open/Save" dialogue window buttons in VBA
提问by pmr
We need to download file from a NASDAQ website automatically. My existing VBA code is opening an IE "Do you want to Open/Save" dialogue window. How to click on that save button and give a path via VBA ? I have tried various windows api methods described in this link herealso but that is giving a result of "Window Not Found".
我们需要自动从纳斯达克网站下载文件。我现有的 VBA 代码正在打开一个 IE“你想打开/保存”对话窗口。如何单击该保存按钮并通过 VBA 给出路径?我试图在这个环节中描述的各种Windows API的方法在这里也不过是给“窗口未找到”的结果。
My current code is as below:
我目前的代码如下:
Sub MyIEauto()
Dim ieApp As InternetExplorer
Dim ieDoc As Object
'Dim ieTable As Object
'create a new instance of ie
Set ieApp = New InternetExplorer
'you don't need this, but it's good for debugging
ieApp.Visible = True
'assume we're not logged in and just go directly to the login page
ieApp.Navigate "https://indexes.nasdaqomx.com/Account/LogOn"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop
Set ieDoc = ieApp.Document
'fill in the login form – View Source from your browser to get the control names
With ieDoc.forms(0)
.UserName.Value = "xxxxxxx"
.Password.Value = "xxxxxxx"
.submit
End With
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.readyState = READYSTATE_COMPLETE: DoEvents: Loop
'now that we're in, go to the page we want
ieApp.Navigate "https://indexes.nasdaqomx.com/Index/ExportWeightings/NDX?tradeDate=2015-08-19T00:00:00.000&timeOfDay=SOD/SODWeightings_2015"
'next below line commented as it is failing
'ieApp.ExecWB 4, 2, "D:\VBA code work\SODWeightings_20150819_NDX.xlsx"
set ieApp=Nothing
set ieDoc=Nothing
End Sub
The screenshot below shows where I have reached. How do I progress from here?
下面的屏幕截图显示了我到达的地方。我如何从这里进步?
回答by pmr
It's solved finally...
终于解决了...
Option Explicit
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, _
ByVal lpsz2 As String) As Long
Public Sub AddReference()
ThisWorkbook.VBProject.References.AddFromFile "C:\Windows\SysWOW64\UIAutomationCore.dll"
End Sub
'after my original code as posted in question then this below lines
Dim o As IUIAutomation
Dim e As IUIAutomationElement
Set o = New CUIAutomation
Dim h As Long
h = ieApp.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
回答by Tony L.
Another way to do this is to send the keystrokes of the shortcut keys to click the save button in IE11. I should note your IE window will need to be the active window for this to work. Thus, it won't work while in debug mode.
另一种方法是将快捷键的击键发送到 IE11 中单击保存按钮。我应该注意到你的 IE 窗口需要是活动窗口才能工作。因此,它在调试模式下不起作用。
The code below calls the shortcut key. I'm just showing the shortcut key so you have a better idea what's happening.
下面的代码调用快捷键。我只是显示快捷键,以便您更好地了解发生了什么。
- Shortcut key:Alt+S
- VBA:
Application.SendKeys "%{S}"
- 快捷键:Alt+S
- VBA:
Application.SendKeys "%{S}"
回答by Johan de Wispelaere
as ieApp.hWnd in a 64bit environment is LongLong, where h is Long this yields a Type Mismatch which can easily been solved by
因为 ieApp.hWnd 在 64 位环境中是 LongLong,其中 h 是 Long 这会产生类型不匹配,可以通过以下方式轻松解决
h = Clng(ieApp.hWnd)
回答by Flash
SendKeys was the solution for me.
SendKeys 是我的解决方案。
myfile = "C:\Users\User\Downloads\myfile.xls"
checkmyfile = Dir(myfile, vbArchive)
Do While checkmyfile = ""
On Error Resume Next
checkmyfile = Dir(myfile , vbArchive)
If checkmyfile = "myfile.xls" Then Exit Do
AppActivate "Title - Internet Explorer"
SendKeys "%(g)"
Application.Wait Now + TimeValue("0:0:1")
Loop


