为 IE9 (vba) 自动保存对话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26038165/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:39:27  来源:igfitidea点击:

Automate saveas dialogue for IE9 (vba)

vbainternet-explorerautomation

提问by ramses1592

I am trying to download an excel sheet from a website. I have thus far achieved until clicking the download button automatically (web scraping). Now ie9 is popping a save as screen. How do i automate that?

我正在尝试从网站下载 Excel 表格。到目前为止,我已经实现了,直到自动单击下载按钮(网络抓取)。现在 ie9 正在弹出另存为屏幕。我如何自动化?

回答by Lifewithsun

You may try this as it is worked for me on IE9:

你可以试试这个,因为它在 IE9 上对我有用:

For below showed download

对于下面显示的下载

  1. Copy file C:\Windows\System32\UIAutomationCore.dllfile to users Documents i.e C:\Users\admin\Documentsthen add reference UIAutomationClientto your macro file.
  2. Paste below code in your module:

        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, "Save")
    
        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   
    
  1. 将文件C:\Windows\System32\UIAutomationCore.dll文件复制到用户文档,即C:\Users\admin\Documents然后添加UIAutomationClient对您的宏文件的引用。
  2. 将以下代码粘贴到您的模块中:

        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, "Save")
    
        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   
    

Try at your end.

在你的最后尝试。

回答by Tuhinansu Gourav

'This is a working code for vba in excel 2007 to open a file
'But you need to add the "UIAutomationCore.dll" to be copied 
'from "C:\Windows\System32\UIAutomationCore.dll" into the 
'path "C:\Users\admin\Documents"    
'The path where to copy may be different and you can find it when you check on 
'the box for UIAutomationClient - the location is given under it.
'Tools-references

Option Explicit
  Dim ie As InternetExplorer
  Dim h As LONG_PTR
  Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LONG_PTR, ByVal hWnd2 As LONG_PTR, ByVal lpsz1 As String, ByVal lpsz2 As String) As LONG_PTR


Sub click_open()
  Dim o As IUIAutomation
  Dim e As IUIAutomationElement
  Dim sh
  Dim eachIE

Do

    Set sh = New Shell32.Shell
     For Each eachIE In sh.Windows
         ' Check if this is the desired URL

    ' Here you can use your condition except .html
    ' If you want to use your URL , then put the URL below in the code for condition check.
    ' This code will reassign your IE object with the same reference for navigation and your issue will resolve.
         If InStr(1, eachIE.LocationURL, "<enter your page url>") Then
         Set ie = eachIE
         Exit Do
         End If
     Next eachIE
 Loop

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 IE11.

我将快捷键发送到 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. The shortcut keys and how to send them are below.

注意:如果 IE 不是您机器上的活动窗口,代码将不会按您的预期运行,因此它在调试模式下将无法运行。快捷键和如何发送它们如下。

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