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

pmr*_*pmr 4 excel internet-explorer vba excel-vba

我们需要自动从纳斯达克网站下载文件.我现有的VBA代码正在打开IE"你想打开/保存"对话窗口.如何点击该保存按钮并通过VBA提供路径?我试图在这个环节中描述的各种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
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

pmr*_*pmr 14

终于解决了......

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
Run Code Online (Sandbox Code Playgroud)


Ton*_* L. 4

另一种方法是发送快捷键的击键来单击 IE11 中的“保存”按钮。我应该指出,您的 IE 窗口需要成为活动窗口才能正常工作。因此,它在调试模式下无法工作。

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

  • 快捷键:Alt+S
  • 编程语言:Application.SendKeys "%{S}"