我正在尝试从网站下载excel表。到目前为止,我已经实现了自动点击下载按钮(网页抓取)。现在ie9正在弹出一个保存为屏幕。我如何自动化?
答案 0 :(得分:11)
你可以尝试这个,因为它在IE9上为我工作:
C:\Windows\System32\UIAutomationCore.dll
文件复制到用户文档,例如C:\Users\admin\Documents
,然后将参考UIAutomationClient
添加到您的宏文件中。 粘贴模块中的代码:
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 :(得分:0)
'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
答案 2 :(得分:0)
我将快捷键发送到IE11。
注意:如果IE不是计算机上的活动窗口,则代码将无法按预期运行,因此在调试模式下它将无法运行。快捷键以及如何发送它们如下。
Application.SendKeys "%{S}"