LoginSignup
1
2

Excel VBAでUIAutomationを用いて画面の要素を調査_ver.2.0

Posted at

概要

  • VBAでUIAutomationを用いて、UI要素を調べるためのソースコードです。以下の動画で使われているものです。

また、以下の動画も合わせて確認すると、わかりやすいと思います。

動作前の設定

  • 必ずVBEの参照設定でUIAutomationClientの参照を有効化してください。
    image.png

実行環境

以下の環境で動作確認をしました。

  • Windows11でのExcel 2021

注意点

  • プログラムの実行については、すべて自己責任で行ってください。実行により発生した、いかなる直接的または間接的被害について、作者はその責任を負いません。

コードの簡単な解説

プログラムの大まかな流れは以下のとおりです。

  1. ウインドウ名からウインドウハンドルを取得
  2. ウインドウ内のすべての要素を取得
  3. それぞれの要素の名前やコントロールタイプをデバッグする

実行方法

実行時は「CheckAllElementsWithUIAutomation」サブプロシージャ内の「Call GetAllElements( , )」の引数を指定して、当該プロシージャを実行します。

  • 第一引数(必須)は、ウィンドウ名(名前の一部でも可能)を指定してください。同じウィンドウ名のものが二つ以上開かれていないことを確認してください。

  • 第二引数(省略可能)は、要素名を入力してください。第二引数を指定すると、当該要素名を含む要素が出現した際に、プロシージャの実行が止まります。

実行例

  • コードに記載されている「Call GetAllElements("Challenge", "住所")」は、記載例です。
  • これを実行すると、「Challenge」という名前がつく最初のウインドウを取得し、「住所」という要素名のものが出現したらデバッグが止まります。
    ※「Challenge」という名前がつくウインドウが複数ないことを確認したうえで、実行します。

各識別子について

  • ControlTypeIdは以下のサイトを参照して、記載しました。

  • ControlPatternは以下のサイトからよく使うものを抜粋しました。

コード_ver.2.0

VBA
Option Explicit

' GetNextWindow関数の宣言
Declare PtrSafe Function GetNextWindow Lib "user32" Alias "GetWindow" _
  (ByVal hwnd As LongPtr, ByVal wFlag As Long) As LongPtr
' GetNextWindow関数は、指定されたウィンドウの次のウィンドウのハンドルを取得する関数です。

' IsWindowVisible関数の宣言
Declare PtrSafe Function IsWindowVisible Lib "user32" _
  (ByVal hwnd As LongPtr) As LongPtr
' IsWindowVisible関数は、指定されたウィンドウが可視状態かどうかを判定する関数です。

' GetWindowText関数の宣言
Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
  (ByVal hwnd As LongPtr, ByVal lpString As String, ByVal cch As Long) As LongPtr
' GetWindowText関数は、指定されたウィンドウのテキストを取得する関数です。

' FindWindow関数の宣言
Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" _
  (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
' FindWindow関数は、指定されたクラス名やウィンドウ名からウィンドウのハンドルを取得する関数です。

Dim hwnd As LongPtr ' ウィンドウのハンドルを保持する変数

Const GW_HWNDLAST = 1 ' 最後のウィンドウ
Const GW_HWNDNEXT = 2 ' 次のウィンドウ

' getWindowHandle関数(独自関数)
Function getWindowHandle(ByVal PartialWindowName As String) As LongPtr
    Dim strCaption As String * 500 ' ウィンドウのテキストを保持する変数
    hwnd = FindWindow(vbNullString, vbNullString) ' 最初のウィンドウのハンドルを取得
    Dim cnt As Long ' カウンタ変数
    cnt = 0
    Dim cap As String ' ウィンドウのテキストを保持する変数
    Do
        If IsWindowVisible(hwnd) Then ' ウィンドウが可視状態かどうかを判定
            GetWindowText hwnd, strCaption, Len(strCaption) ' ウィンドウのテキストを取得
            cap = Left(strCaption, InStr(strCaption, vbNullChar) - 1) ' ヌル文字までの部分を抽出
            If InStr(cap, PartialWindowName) <> 0 Then ' ウィンドウのテキストに指定の文字列が含まれているかを判定
                getWindowHandle = hwnd ' ウィンドウのハンドルを返す
                Exit Function ' 関数を終了
            End If
        End If
        hwnd = GetNextWindow(hwnd, GW_HWNDNEXT) ' 次のウィンドウのハンドルを取得
        DoEvents ' イベントを処理
        If hwnd = GetNextWindow(hwnd, GW_HWNDLAST) And cnt = 0 Then ' 最後のウィンドウで初回の場合
            cnt = 1
        ElseIf hwnd = GetNextWindow(hwnd, GW_HWNDLAST) And cnt = 1 Then ' 最後のウィンドウで2回目の場合
            Debug.Print "ウィンドウ取得に失敗" ' デバッグウィンドウにメッセージを出力
            Application.Wait [Now()] + (1 / 86400) ' ウィンドウハンドル失敗用の待機
            hwnd = FindWindow(vbNullString, vbNullString) ' 最初のウィンドウのハンドルを取得し直す
            cnt = 0 ' カウンタをリセット
        End If
    Loop
End Function

Function GetAllElements(ByVal PartialWindowName As String, Optional ByVal searchWord As String) As String
    ' UIAutomationのインスタンスを作成
    Dim uiAuto As UIAutomationClient.CUIAutomation  ' UIAutomationのインスタンスを格納する変数
    Set uiAuto = New UIAutomationClient.CUIAutomation  ' 新しいUIAutomationのインスタンスを作成

    ' ウィンドウのハンドルを取得
    Dim hwnd As LongPtr  ' ウィンドウのハンドルを格納する変数
    hwnd = getWindowHandle(PartialWindowName)  ' 指定された部分ウィンドウ名のウィンドウのハンドルを取得

    ' ウィンドウ要素を取得
    Dim elm01 As UIAutomationClient.IUIAutomationElement  ' ウィンドウ要素を格納する変数
    Set elm01 = uiAuto.ElementFromHandle(ByVal hwnd)  ' ウィンドウのハンドルからUIAutomation要素を取得

    ' 条件を作成
    Dim uiCnd As IUIAutomationCondition  ' 条件を格納する変数
    Set uiCnd = uiAuto.CreateTrueCondition  ' 真の条件を作成(CreateTrueConditionは、常に真となる条件を作成します。特定の条件を満たす要素を検索するのではなく、全ての要素を取得する際に利用されます。)

    ' UIAutomationの要素を格納するための配列を宣言し、要素を取得する
    Dim aryElm As UIAutomationClient.IUIAutomationElementArray  ' aryElmはUIAutomationの要素を格納する変数(配列)です
    Set aryElm = elm01.FindAll(TreeScope_Subtree, uiCnd)  ' elm01(ウィンドウ要素)内のサブツリー全体から条件(uiCnd)に一致する要素を取得し、aryElmに格納

    ' デバッグ用にループ内の要素情報を表示
    Dim i As Long

    ' aryElm内の各要素に対して処理を行うループ
    For i = 0 To aryElm.Length - 1
        Application.Wait [Now()] + (0.05 / 86400)     ' 簡単なウェイト(待機)を挿入することで、処理をゆっくり進める
        Debug.Print "i=" & i    ' デバッグ用に現在のインデックスを表示
        Debug.Print "   要素名:" & aryElm.GetElement(i).CurrentName    ' デバッグ用に要素の名前を表示
        Call GetControlTypeByValue(aryElm.GetElement(i).CurrentControlType)    ' 要素のコントロールタイプを表示する関数を呼び出す
        Call GetUIPatternAndDebug(aryElm.GetElement(i))    ' UI パターンの情報を表示する関数を呼び出す

        ' 検索ワードが指定されており、要素の名前が検索ワードを含む場合は停止
        If searchWord <> "" And InStr(aryElm.GetElement(i).CurrentName, searchWord) > 0 Then
            Stop
        End If

        ' 検索ワードが指定されていない場合、50回ごとに停止
        If searchWord = "" And i Mod 50 = 0 And i <> 0 Then
            Stop
        End If
    Next i
End Function


Function GetControlTypeByValue(ByVal controlTypeCode As Long) As String

    ' コントロール型識別子の値(既定値)からコントロールの種類の名前に変換
    Select Case controlTypeCode
        Case 50040
            GetControlTypeByValue = "AppBar"
        Case 50000
            GetControlTypeByValue = "Button"
        Case 50001
            GetControlTypeByValue = "Calendar"
        Case 50002
            GetControlTypeByValue = "CheckBox"
        Case 50003
            GetControlTypeByValue = "ComboBox"
        Case 50025
            GetControlTypeByValue = "Custom"
        Case 50028
            GetControlTypeByValue = "DataGrid"
        Case 50029
            GetControlTypeByValue = "DataItem"
        Case 50030
            GetControlTypeByValue = "Document"
        Case 50004
            GetControlTypeByValue = "Edit"
        Case 50026
            GetControlTypeByValue = "Group"
        Case 50034
            GetControlTypeByValue = "Header"
        Case 50035
            GetControlTypeByValue = "HeaderItem"
        Case 50005
            GetControlTypeByValue = "Hyperlink"
        Case 50006
            GetControlTypeByValue = "Image"
        Case 50008
            GetControlTypeByValue = "List"
        Case 50007
            GetControlTypeByValue = "ListItem"
        Case 50010
            GetControlTypeByValue = "MenuBar"
        Case 50009
            GetControlTypeByValue = "Menu"
        Case 50011
            GetControlTypeByValue = "MenuItem"
        Case 50033
            GetControlTypeByValue = "Pane"
        Case 50012
            GetControlTypeByValue = "ProgressBar"
        Case 50013
            GetControlTypeByValue = "RadioButton"
        Case 50014
            GetControlTypeByValue = "ScrollBar"
        Case 50039
            GetControlTypeByValue = "SemanticZoom"
        Case 50038
            GetControlTypeByValue = "Separator"
        Case 50015
            GetControlTypeByValue = "Slider"
        Case 50016
            GetControlTypeByValue = "Spinner"
        Case 50031
            GetControlTypeByValue = "SplitButton"
        Case 50017
            GetControlTypeByValue = "StatusBar"
        Case 50018
            GetControlTypeByValue = "Tab"
        Case 50019
            GetControlTypeByValue = "TabItem"
        Case 50036
            GetControlTypeByValue = "Table"
        Case 50020
            GetControlTypeByValue = "Text"
        Case 50027
            GetControlTypeByValue = "Thumb"
        Case 50037
            GetControlTypeByValue = "TitleBar"
        Case 50021
            GetControlTypeByValue = "ToolBar"
        Case 50022
            GetControlTypeByValue = "ToolTip"
        Case 50023
            GetControlTypeByValue = "Tree"
        Case 50024
            GetControlTypeByValue = "TreeItem"
        Case 50032
            GetControlTypeByValue = "Window"
    ' 上記のいずれのケースにも一致しない場合、未知のコントロールタイプとして空文字列を返します。
        Case Else
            ' 未知のコントロールタイプの場合
            GetControlTypeByValue = "" ' 未知のコントロールタイプです。
    End Select

    ' 結果をデバッグ出力
    Debug.Print "   要素のコントロールタイプ:" & GetControlTypeByValue  ' 結果をデバッグウィンドウに出力します。
End Function


' UI要素のパターンを取得し、それに基づいてデバッグメッセージを出力する関数
Function GetUIPatternAndDebug(ByVal uiElm As UIAutomationClient.IUIAutomationElement)
    ' uiElm: UIAutomation要素オブジェクト

    ' UI要素を展開/折り畳みするためのパターン
    Dim UIExpandCollapsePattern As IUIAutomationExpandCollapsePattern
    Set UIExpandCollapsePattern = uiElm.GetCurrentPattern(10005)

    ' UI要素を起動するためのパターン
    Dim UIInvokePattern As IUIAutomationInvokePattern
    Set UIInvokePattern = uiElm.GetCurrentPattern(10000)

    ' UI要素を選択するパターン
    Dim UISelectionItemPattern As IUIAutomationSelectionItemPattern
    Set UISelectionItemPattern = uiElm.GetCurrentPattern(10010)

    ' UI要素のオンとオフの状態を切り替えるためのパターン
    Dim UITogglePattern As IUIAutomationTogglePattern
    Set UITogglePattern = uiElm.GetCurrentPattern(10015)

    ' UI要素の値を設定/取得するためのパターン
    Dim UIValuePattern As IUIAutomationValuePattern
    Set UIValuePattern = uiElm.GetCurrentPattern(10002)
    
    ' UI要素をスクロールするためのパターン
    Dim UIScrollPattern As IUIAutomationScrollPattern
    Set UIScrollPattern = uiElm.GetCurrentPattern(10004)
    
    ' 各パターンが取得できているかを確認し、デバッグメッセージを出力
    If Not UIExpandCollapsePattern Is Nothing Then
        Debug.Print "   可能な操作:ExpandCollapse"
    End If
    If Not UIInvokePattern Is Nothing Then
        Debug.Print "   可能な操作:Invoke"
    End If
    If Not UISelectionItemPattern Is Nothing Then
        Debug.Print "   可能な操作:SelectionItem"
    End If
    If Not UITogglePattern Is Nothing Then
        Debug.Print "   可能な操作:Toggle"
    End If
    If Not UIValuePattern Is Nothing Then
        Debug.Print "   可能な操作:Value"
    End If
    If Not UIScrollPattern Is Nothing Then
        Debug.Print "   可能な操作:Scroll"
    End If
    
End Function

' UIAutomationを使用して、指定された部分ウィンドウ名内のUI要素を取得し、デバッグ表示するサブプロシージャ
Sub CheckAllElementsWithUIAutomation()
    ' GetAllElements関数を呼び出し、指定された部分ウィンドウ名と検索ワードを渡す
    Call GetAllElements("Challenge", "住所")
End Sub


コードの修正履歴

1
2
2

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
1
2