【VBA】UIAutomationで業務改善4
Microsoft Edgeの操作
仕事中に読書もブログでけてないjimです。どうも✋
今回は、UIAutomationでMicrosoft Edgeを操作してみようのコーナーです。
InternetexplorerがオワコンでSeleniumbasicも使えない人のために、日曜の朝からせっせとコード書きました。
うん、やっつけだよ👍
つか、自分もEdgeのドライバーDLさせてもらってないので、その内の一人なんですけどね😣
Edgeのホームが『Bing』って設定で説明していきます。
【VBA】UIAutomationで業務改善1で紹介しUIAutomationSPYで要素を調べます。
↓のNameを取得したいんだけど、よくわからんのよね。。。

↓Hierarchy見るとTypeIdも見れるのだけど、よくわからんのよねぇ。。。

コードを書き散らす
エレメント.FindFirstじゃ取得できないということ?
よくわからないから、一番最初にWin配下の要素を全取得⇒その後、ゴニョゴニョしていきやす。。。
↓参照設定が必要です

↓書き散らした
Option Explicit
Private Declare Function FindWindowA Lib "user32" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Sub UIAutomation()
Dim shellObject As Object
Dim uiAuto As UIAutomationClient.CUIAutomation
Dim edgeElement As IUIAutomationElement
Dim editElement As IUIAutomationElement
Dim valuePattern As IUIAutomationValuePattern
Dim invokePattern As IUIAutomationInvokePattern
'edge立ち上げる(やっつけ)
Set shellObject = CreateObject("Shell.Application")
shellObject.ShellExecute "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Edge"
'新しいタブを取得するまで待機(やっつけ)
Do While GetHwnd("新しいタブ") = 0
DoEvents
Loop
'edgeを取得(やっつけ)
Set uiAuto = New UIAutomationClient.CUIAutomation
Set edgeElement = GetEdge(uiAuto)
'検索editに文言入力(やっつけ)
Set editElement = GetElement(uiAuto, edgeElement, UIA_NamePropertyId, "検索語句を入力", UIA_EditControlTypeId)
Set valuePattern = editElement.GetCurrentPattern(UIA_ValuePatternId)
valuePattern.SetValue "崖っぷち派遣社員"
'検索button押す(やっつけ)
Set editElement = GetElement(uiAuto, edgeElement, UIA_NamePropertyId, "検索", UIA_ButtonControlTypeId)
Set invokePattern = editElement.GetCurrentPattern(UIA_InvokePatternId)
invokePattern.Invoke
End Sub
Function GetHwnd(ByVal myStr As String)
GetHwnd = FindWindowA(vbNullString, myStr)
End Function
Function GetEdge(ByVal uiAuto As UIAutomationClient.CUIAutomation)
Dim allElement As IUIAutomationElement
Dim condControls As UIAutomationClient.IUIAutomationCondition
Dim arryControls As UIAutomationClient.IUIAutomationElementArray
Dim i As Long
Set allElement = uiAuto.GetRootElement
Set condControls = uiAuto.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_WindowControlTypeId)
Set arryControls = allElement.FindAll(TreeScope_Subtree, condControls)
For i = 0 To arryControls.Length - 1
If LCase(arryControls.GetElement(i).CurrentName) Like "*- microsoft? edge" And _
arryControls.GetElement(i).CurrentClassName = "Chrome_WidgetWin_1" Then
Set GetEdge = arryControls.GetElement(i)
Exit For
End If
Next
End Function
Function GetElement(ByVal uiAuto As UIAutomationClient.CUIAutomation, ByVal edgeElement As IUIAutomationElement, _
ByVal propertyId As Long, ByVal propertyString As String, Optional ByVal propertyType As Long = 0)
Dim Cond1 As IUIAutomationCondition
Dim Cond2 As IUIAutomationCondition
Set Cond1 = uiAuto.CreatePropertyCondition(propertyId, propertyString)
Set Cond2 = uiAuto.CreatePropertyCondition(UIA_ControlTypePropertyId, propertyType)
Set Cond1 = uiAuto.CreateAndCondition(Cond1, Cond2)
Set GetElement = edgeElement.FindFirst(TreeScope_Subtree, Cond1)
End Function
やっつけで作ったので、別ウィンドウのことや待機処理のことは無視してます。
あと、Edgeのパス違うかもしれないから注意です。
ここまでやったら、、後は何とかなるでしょ?ならん?
ってことで、1年ぶり以上の『UIAutomation』ネタでした😉
自分もはよ職場のヤーツなんとかしないと。。。
したら、またね✋



ディスカッション
コメント一覧
まだ、コメントがありません