excel - 运行时错误;自动化错误;使用 UIAutomation 的 VBA 中的未指定错误
问题描述
它以前可以工作,但现在当我在 sub 的第 12 行设置 oElements 时,我不断收到错误代码“自动化错误;未指定错误”。有什么想法吗?
Sub SavePath(ByVal strWindowID As String, ByVal strObjectName As String, ByVal strAutomationId As String, ByVal strLocalizeType As String, ByVal strValue As String)
Dim intElementCounter As Integer
Dim strTreeItem1 As String
Dim strTreeitem2 As String
Dim strTreeitem3 As String
strThiswbFileName = ActiveWorkbook.Name
strThiswbCaption = Application.Caption
Set oTW = oAutomation.ControlViewWalker
Set oCondition = oAutomation.CreatePropertyCondition(UIAutomationClient.UIA_NamePropertyId, strObjectName)
Set oElements = oAutomation.GetRootElement.FindAll(TreeScope_Descendants, oCondition)
For intElementCounter = 0 To oElements.length - 1
If oElements.GetElement(intElementCounter).CurrentName = strObjectName Then
If oElements.GetElement(intElementCounter).CurrentAutomationId = strAutomationId Then
If oElements.GetElement(intElementCounter).CurrentLocalizedControlType = strLocalizeType Then
Set oPatternValue = oElements.GetElement(intElementCounter).GetCurrentPattern(UIAutomationClient.UIA_ValuePatternId)
oPatternValue.SetValue strValue
Exit Sub
End If
End If
End If
Next
End Sub
我的参考是
- 应用程序的 Visual Basic
- Microsoft Excel 14.0 对象库
- OLE 自动化
- Microsoft ActiveX 数据对象 2.0 库
- Microsoft HTML 对象库
- 微软互联网控制
- Microsoft Visual Basic for Applications 可扩展性 5.3
- UIAutomationClient
解决方案
不要随意使用来自 RootElement 的 treescope_descendants!我不知道为什么,但 FindAll 函数中可能会出现错误,通常是在遍历根的后代时。
假设您正在寻找“另存为”对话框(然后是其中的文件路径文本字段)。
遍历 rootelement.children,并查找“另存为”窗口的所有者。因此,如果所有者是 Internet Explorer 浏览器,请检查 rootelement.children 中是否有 UIA_Nameproperty(如“*Internet Explorer”)。然后在他们的后代中搜索 SaveAs 框。
或者,尝试在设置 oElements 时添加不同的条件,以获取具有 windowpattern 的条件:
Set oCondition = oAutomation.CreatePropertyCondition(UIAutomationClient.UIA_IsWindowPatternAvailablePropertyId, True)
您已经在第一个 if 块中再次检查 nameproperty
推荐阅读
- java - 在java中嵌套XML标签时面临问题
- pymc3 - 在 Anaconda 3 / Win 上安装 PyMC3
- redmine - Redmine 中的 Backlogs 插件出现错误
- ios - iOS侧载expo应用水平翻转
- sql - SQL查询查找表中最后创建的记录
- javascript - 如何使用 React.js 将 state 属性传递给构造函数中的子组件?
- tensorflow - 我没有让 keras fit_generator 与混合(图像和数字)输入一起工作
- html - 如何逐个移动元素
- python - 上下文不打印 Django
- r - error en METRIC.EB .local(x, …) 中的错误:RasterLayer 没有 NA 像元(要计算距离)