Office関連

起動中のMicrosoft EdgeからタイトルとURLを取得するVBAマクロ(UI Automation編)

当ブログでは、Microsoft Edgeを外部から操作するプログラムについて、いくつか記事を書いてきましたが、今回は起動中のEdgeから開いているWebページのタイトルをURLを取得してみたいと思います。今回使うのは VBA + UI Automationです。

起動中のMicrosoft EdgeからタイトルとURLを取得するVBAマクロ

Option Explicit

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Public Sub ListEdgeTabInfo()
'起動中のEdgeのタブからタイトルとURLを取得
'※ UIAutomationClient(UIAutomationCore.dll)要参照
'※ Edgeが最小化状態の場合は実行不可
  Dim uiAuto As UIAutomationClient.CUIAutomation
  Dim elmDesktop As UIAutomationClient.IUIAutomationElement
  Dim elmEdge As UIAutomationClient.IUIAutomationElement
  Dim elmCoreWindow As UIAutomationClient.IUIAutomationElement
  Dim elmTabsList As UIAutomationClient.IUIAutomationElement
  Dim cndWindowControls As UIAutomationClient.IUIAutomationCondition
  Dim cndCoreWindow As UIAutomationClient.IUIAutomationCondition
  Dim cndTabsList As UIAutomationClient.IUIAutomationCondition
  Dim cndListItems As UIAutomationClient.IUIAutomationCondition
  Dim aryWindowControls As UIAutomationClient.IUIAutomationElementArray
  Dim aryListItems As UIAutomationClient.IUIAutomationElementArray
  Dim ptnSelectionItem As UIAutomationClient.IUIAutomationSelectionItemPattern
  Dim i As Long
  
  'デスクトップ取得
  Set uiAuto = New UIAutomationClient.CUIAutomation
  Set elmDesktop = uiAuto.GetRootElement
  
  'Edge取得
  Set cndWindowControls = uiAuto.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_WindowControlTypeId)
  Set aryWindowControls = elmDesktop.FindAll(TreeScope_Subtree, cndWindowControls)
  For i = 0 To aryWindowControls.Length - 1
    If LCase(aryWindowControls.GetElement(i).CurrentName) Like "*microsoft edge" And _
       aryWindowControls.GetElement(i).CurrentClassName = "ApplicationFrameWindow" Then
      Set elmEdge = aryWindowControls.GetElement(i)
      Exit For
    End If
  Next
  If elmEdge Is Nothing Then Exit Sub
  
  '各タブ取得
  Set cndCoreWindow = uiAuto.CreatePropertyCondition(UIA_ClassNamePropertyId, "Windows.UI.Core.CoreWindow")
  Set elmCoreWindow = elmEdge.FindFirst(TreeScope_Subtree, cndCoreWindow)
  If elmCoreWindow Is Nothing Then Exit Sub
  Set cndTabsList = uiAuto.CreatePropertyCondition(UIA_AutomationIdPropertyId, "TabsList")
  Set elmTabsList = elmCoreWindow.FindFirst(TreeScope_Subtree, cndTabsList)
  If elmTabsList Is Nothing Then Exit Sub
  Set cndListItems = uiAuto.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_ListItemControlTypeId)
  Set aryListItems = elmTabsList.FindAll(TreeScope_Subtree, cndListItems)
  For i = 0 To aryListItems.Length - 1
    Set ptnSelectionItem = aryListItems.GetElement(i).GetCurrentPattern(UIA_SelectionItemPatternId)
    ptnSelectionItem.Select 'タブ選択
    Sleep 300
    Debug.Print aryListItems.GetElement(i).CurrentName, GetTabURL(uiAuto, elmCoreWindow)
  Next
End Sub

Private Function GetTabURL(ByVal uiAuto As UIAutomationClient.CUIAutomation, _
                           ByVal elmCoreWindow As UIAutomationClient.IUIAutomationElement) As String
'[検索または Web アドレスを入力]からURL取得
  Dim elm As UIAutomationClient.IUIAutomationElement
  Dim cnd As UIAutomationClient.IUIAutomationCondition
  Dim url As String
  
  url = "" '初期化
  On Error Resume Next
  Set cnd = uiAuto.CreatePropertyCondition(UIA_AutomationIdPropertyId, "addressEditBox")
  Set elm = elmCoreWindow.FindFirst(TreeScope_Subtree, cnd)
  elm.SetFocus
  Sleep 300
  Set elm = elmCoreWindow.FindFirst(TreeScope_Subtree, cnd) 'フォーカス後再取得
  url = elm.GetCurrentPropertyValue(UIA_ValueValuePropertyId)
  On Error GoTo 0
  GetTabURL = url
End Function

AutomateMicrosoftEdge_UIA_01

やっていることは、起動中のEdgeを取得 → 各タブを取得 → 順番にタブをクリック → [検索または Web アドレスを入力]欄からURLを取得、という作業の自動化なのですが、正直途中で投げ出したくなりました。

外部のアプリケーションを操作するのにUI Automationはとても便利なのですが、Inspectで構造を一つずつ確認して、各アイテムのプロパティやクラス名を調べて…と、基本的に地味で面倒な作業の連続です。

しかも、本当はAPI関数を使いたくなかったので、わざわざアプリケーションのウィンドウハンドルからデスクトップを辿って…という遠回りなことをしたわけですが、どうしてもタブ選択・アドレス欄のフォーカス処理待ちが上手くいかなかったので、仕方なくSleepを使うことにしました。

というわけで、

“VBA + UI AutomationでEdgeをアレコレ操作するのは止めた方が良い”

というのが私の感想ですが、こんなコードもどなたかの参考になれば幸いです。

関連記事

続・Microsoft Edgeを操作するVBAマクロ(DOM編)前のページ

起動中のMicrosoft EdgeからタイトルとURLを取得するVBAマクロ(DOM編)次のページ

関連記事

  1. Office関連

    オフィス祭り 2018 in 東京が9月15日(土)に開催されます。

    突然ですが、私はMicrosoft Officeが大好きです。20…

  2. Office関連

    ディスプレイのサイズを取得するVBAマクロ

    「VBA ディスプレイ 幅 高さ」といったキーワード検索でのアクセスが…

  3. Office関連

    「2014年12月のWindows Update以降コマンドボタンが使えなくなった」トラブルへのFi…

    当ブログでも「KB2553154の更新プログラムをアンインストールする…

  4. Office関連

    [Office 2013]サインインを無効にする。

    2013/4/10 追記:「オンライン画像」や「Office 用ア…

  5. アイコン一覧

    Office 2013 アイコン一覧(N)

    ・Office 2013 アイコン一覧 NUM…

  6. Windows関連

    [Windows 8]管理者権限でコマンドプロンプトを実行する。

    ※ 下記はWindows Developer Preview(英語版・…

コメント

  • コメント (0)

  • トラックバックは利用できません。

  1. この記事へのコメントはありません。

Time limit is exhausted. Please reload CAPTCHA.

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP