2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ExcelVBAで、(WinAPIを使用せずに)UIAutomationで画面要素を取得<文字を頼りに探す>

Last updated at Posted at 2021-03-13

##ExcelVBAで、(WinAPIを使用せずに)UIAutomationで画面要素を取得<文字を頼りに探す>

たとえば、画面の中の、ボタンなどを、文字を頼りに、
要素(UIAutomationElement)を探します。

image.png

ここでは、「アクティブ時間」という文字で探してます。
結果は、イミディエイトウィンドウに出力します。
発見された要素から、順に親をたどっています。
上から下に行くにつれて、各要素から、より根側の要素へとさかのぼっています。
image.png

sample.bas

'/---------------------------------------------------
'参照設定:UIAutomationClient

'/---------------------------------------------------
Sub main()
    ScWd = "アクティブ時間"
    findElmnt (ScWd)
End Sub

'/---------------------------------------------------
Sub findElmnt(ScWd)

Dim uia As New UIAutomationClient.CUIAutomation

Dim cnd As IUIAutomationCondition
Dim ret As IUIAutomationElementArray

Set cnd = uia.CreatePropertyCondition(UIA_PropertyIds.UIA_IsOffscreenPropertyId, False)

Do
    Application.Wait [Now() + "0:00:00.1"]
    DoEvents
    Set ret = uia.GetRootElement.FindAll(TreeScope_Element Or TreeScope_Children Or TreeScope_Descendants Or TreeScope_Ancestors Or TreeScope_Subtree, cnd)
Loop Until Not ret Is Nothing

'Debug.Print ret.Length

Dim tw As IUIAutomationTreeWalker
Set tw = uia.CreateTreeWalker(cnd)

Dim pe As IUIAutomationElement
For i = 0 To ret.Length - 1

    If InStr(1, ret.GetElement(i).CurrentName, ScWd) > 0 Then

        Debug.Print ""
        Debug.Print ""
        Debug.Print "1 _______________________________"
        Debug.Print ret.GetElement(i).CurrentName & "_" & ret.GetElement(i).CurrentClassName

        Set pe = tw.GetParentElement(ret.GetElement(i))
        Debug.Print "2 _______________________________"
        Debug.Print pe.CurrentName & "_" & pe.CurrentClassName
        
        Do
            Application.Wait [Now() + "0:00:00.1"]
            DoEvents
            Set pe = tw.GetParentElement(pe)
            If Not pe Is Nothing Then
                Debug.Print "3 _______________________________"
                Debug.Print pe.CurrentName & "_" & pe.CurrentClassName
            End If
        Loop Until pe Is Nothing
        
    End If

Next

End Sub

##参考にさせていただいたページ

神ってます

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?