vba 使用 excel 实现 UI 自动化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44756042/
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
UI automation with excel
提问by Dinesh Vilas Pawar
I am new to UI Automation. In my current organisation I was tasked with making an automated tool using GUI(Graphics User Interface) screen reading, but it is not working perfectly with other my colleague's machine because of a difference in screen resolution.
我是 UI 自动化的新手。在我目前的组织中,我的任务是使用 GUI(图形用户界面)屏幕阅读制作自动化工具,但由于屏幕分辨率的差异,它无法与我同事的其他机器完美配合。
I watched thislink on you-tube to try and understand UI Automation with excel, but I can't find much on this topic anywhere else.
我在 You -tube 上观看了此链接,以尝试使用 excel 了解 UI 自动化,但我在其他任何地方都找不到有关此主题的太多信息。
Can anyone direct me toward resources on UI Automation? I Would like to know where I can learn it, read about it, and how to implement it with Excel.
任何人都可以指导我使用 UI 自动化的资源吗?我想知道我在哪里可以学习它,阅读它,以及如何用 Excel 实现它。
Thanks in advance I really appreciate if anyone could help me.
提前致谢,如果有人能帮助我,我真的很感激。
回答by junkew
UIAutomation from Microsoft is very powerfull and works well with windows 7, 8, 10 also from visual basic for applications (32 and 64 bits) and can be handy used to do some nice GUI Automation without expensive tools.
来自 Microsoft 的 UIAutomation 非常强大,适用于 Windows 7、8、10,也适用于 Visual Basic 的应用程序(32 位和 64 位),并且可以方便地用于执行一些不错的 GUI 自动化,而无需昂贵的工具。
Make sure in VBA reference you have UIAutomationCore.Dll references (and weird enough sometimes on some computers you have to copy this to your documents folder)
确保在 VBA 参考中有 UIAutomationCore.Dll 参考(有时在某些计算机上很奇怪,您必须将其复制到文档文件夹中)
Below you can see 2 base examples but as MS Automation is a huge library for all routines you can read a lot on MSDN for full documentation. I use the MS UIA routines in AutoIt and in VBA
您可以在下面看到 2 个基本示例,但由于 MS 自动化是一个包含所有例程的庞大库,您可以在 MSDN 上阅读大量完整文档。我在 AutoIt 和 VBA 中使用 MS UIA 例程
- For AutoIt its shared over here
- 对于 AutoIt 它在这里共享
https://www.autoitscript.com/forum/topic/153520-iuiautomation-ms-framework-automate-chrome-ff-ie/
https://www.autoitscript.com/forum/topic/153520-iuiautomation-ms-framework-automate-chrome-ff-ie/
- For VBA I do not have a standard library but someone did a try with this https://github.com/mhumpher/UIAutomation_VBA
- 对于 VBA,我没有标准库,但有人尝试使用此 https://github.com/mhumpher/UIAutomation_VBA
Option Explicit
选项显式
Sub test()
Dim c As New CUIAutomation
Dim oDesktop As IUIAutomationElement
Set oDesktop = c.GetRootElement
Debug.Print oDesktop.CurrentClassName & vbTab & oDesktop.CurrentName & vbTab & oDesktop.CurrentControlType
End Sub
'Test uia just dumps all windows of the desktop to the debug window
Sub testUIA()
Dim allEl As IUIAutomationElementArray 'Returns an element array with all found results
Dim oElement As IUIAutomationElement 'Reference to an element
Dim ocondition As IUIAutomationCondition
Dim i As Long
Dim x As New clsUIA
'Just reference the three mainly used properties. many more are available when needed
Debug.Print x.oDesktop.CurrentName & x.oDesktop.CurrentClassName & x.oDesktop.CurrentControlType
Set ocondition = x.oCUIAutomation.CreateTrueCondition 'Filter on true which means get them all
Set allEl = x.oDesktop.FindAll(TreeScope_Children, ocondition) 'Find them in the direct children, unfortunately hierarchies are strange sometimes
'Just reference the three mainly used properties. many more are available when needed
For i = 0 To allEl.Length - 1
Set oElement = allEl.GetElement(i)
' If oElement.CurrentClassName = "PuTTY" Then
Debug.Print oElement.CurrentClassName & oElement.CurrentName & oElement.CurrentControlType
' Debug.Print oElement.CurrentBoundingRectangle
oElement.SetFocus
DoEvents
Sleep 2000
' End If
Next
End Sub
回答by Abhishek Gupta
Seems like you are doing the automation using the coordinates, which changes when you switch to other resolution. If this is the case, please automate your application using ID, Class, Xpath, CSS etc. This link will help you in that: http://www.guru99.com/locators-in-selenium-ide.html
似乎您正在使用坐标进行自动化,当您切换到其他分辨率时,坐标会发生变化。如果是这种情况,请使用 ID、Class、Xpath、CSS 等自动化您的应用程序。此链接将帮助您:http: //www.guru99.com/locators-in-selenium-ide.html
For automation using Excel, please look into the following link: http://www.guru99.com/all-about-excel-in-selenium-poi-jxl.html
对于使用 Excel 的自动化,请查看以下链接:http: //www.guru99.com/all-about-excel-in-selenium-poi-jxl.html
回答by Compee
create clsUIA class then insert this code 'clsUIA with some logic like
创建 clsUIA 类,然后使用一些逻辑插入此代码 'clsUIA
'start by add the following code
Dim c As New CUIAutomation
Public oCUIAutomation As New CUIAutomation
Function oDesktop() As IUIAutomationElement
Set oDesktop = c.GetRootElement
End Function