在VBA中控制IE11“是否要打开/保存”对话窗口按钮

时间:2015-08-21 16:43:04

标签: vba excel-vba internet-explorer excel

我们需要自动从纳斯达克网站下载文件。我现有的VBA代码正在打开IE“你想打开/保存”对话窗口。如何点击该保存按钮并通过VBA提供路径? 我已经尝试了此链接here中描述的各种Windows api方法,但是这样做的结果是“找不到窗口”。

我目前的代码如下:

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

下面的屏幕截图显示了我到达的位置。我如何从这里进步?

enter image description here

4 个答案:

答案 0 :(得分:12)

终于解决了......

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

答案 1 :(得分:4)

另一种方法是发送快捷键的按键以单击IE11中的保存按钮。我应该注意你的IE窗口需要是活动窗口才能工作。因此,它在调试模式下不会工作。

以下代码调用快捷键。我只是显示快捷键,以便您更好地了解发生了什么。

  • 快捷键: Alt + S
  • VBA:Application.SendKeys "%{S}"

答案 2 :(得分:0)

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

答案 3 :(得分:0)

因为在64位环境中的ieApp.hWnd是LongLong,其中h是Long 这会产生类型不匹配,可以很容易地通过

解决
h = Clng(ieApp.hWnd)