cn4vep4g

Option Explicit

Public Sub Sample1()
  CloseEdgeWindow "Yahoo" 'ページタイトルに「Yahoo」が含まれるタブをすべて閉じる
End Sub

Public Sub Sample2()
  CloseEdgeWindow 'Microsoft Edgeのウィンドウをすべて閉じる
End Sub

Private Sub CloseEdgeWindow(Optional ByVal pageTitle As String = "")
  Dim uiAuto As CUIAutomation
  Dim elmRoot As IUIAutomationElement
  Dim elmEdgeWindow As IUIAutomationElement
  Dim elmTabBar As IUIAutomationElement
  Dim elmTabStrip As IUIAutomationElement
  Dim elmTab As IUIAutomationElement
  Dim elmTabCloseButton As IUIAutomationElement
  Dim aryWindows As IUIAutomationElementArray
  Dim aryTabItems As IUIAutomationElementArray
  Dim cndTabItems As IUIAutomationCondition
  Dim wptn As IUIAutomationWindowPattern
  Dim iptn As IUIAutomationInvokePattern
  Dim i As Integer, j As Integer
  
  Set uiAuto = New CUIAutomation
  Set elmRoot = uiAuto.GetRootElement
  Set aryWindows = elmRoot.FindAll(TreeScope_Children, uiAuto.CreateTrueCondition)
  For i = 0 To aryWindows.Length - 1
    'Edgeのプロセスのみを処理対象とする
    If LCase(GetProcessName(aryWindows.GetElement(i).CurrentProcessId)) = "msedge.exe" Then
      Set elmEdgeWindow = aryWindows.GetElement(i)
      'ページのタイトルを指定しない場合はウィンドウごと閉じる
      If Len(pageTitle) < 1 Then
        If elmEdgeWindow.GetCurrentPropertyValue(UIA_IsWindowPatternAvailablePropertyId) = True Then
          Set wptn = elmEdgeWindow.GetCurrentPattern(UIA_WindowPatternId)
          wptn.Close
        End If
      Else
        Set elmTabBar = GetElement(uiAuto, _
                                   elmEdgeWindow, _
                                   UIA_ClassNamePropertyId, _
                                   "TabStripRegionView", _
                                   UIA_TabControlTypeId)
        If Not elmTabBar Is Nothing Then
          Set elmTabStrip = GetElement(uiAuto, _
                                       elmTabBar, _
                                       UIA_ClassNamePropertyId, _
                                       "TabStrip", _
                                       UIA_PaneControlTypeId, _
                                       TreeScope_Children)
          If Not elmTabStrip Is Nothing Then
            Set cndTabItems = uiAuto.CreatePropertyCondition(UIA_ControlTypePropertyId, UIA_TabItemControlTypeId)
            Set aryTabItems = elmTabStrip.FindAll(TreeScope_Children, cndTabItems)
            For j = 0 To aryTabItems.Length - 1
              Set elmTab = aryTabItems.GetElement(j)
              '指定したページタイトルのタブを閉じる
              If InStr(elmTab.CurrentName, pageTitle) Then
                Set elmTabCloseButton = GetElement(uiAuto, _
                                                   elmTab, _
                                                   UIA_ClassNamePropertyId, _
                                                   "TabCloseButton", _
                                                   UIA_ButtonControlTypeId, _
                                                   TreeScope_Children)
                If Not elmTabCloseButton Is Nothing Then
                  Set iptn = elmTabCloseButton.GetCurrentPattern(UIA_InvokePatternId)
                  iptn.Invoke
                End If
              End If
            Next
          End If
        End If
      End If
    End If
    DoEvents
  Next
End Sub

Private Function GetProcessName(ByVal pid As Long) As String
  Dim itm As Object
  For Each itm In CreateObject("WbemScripting.SWbemLocator").ConnectServer _
    .ExecQuery("Select * From Win32_Process Where ProcessId = '" & pid & "'")
    GetProcessName = itm.Name
  Next
End Function

Private Function GetElement(ByVal uiAuto As CUIAutomation, _
                            ByVal elmParent As IUIAutomationElement, _
                            ByVal propertyId As Long, _
                            ByVal propertyValue As Variant, _
                            Optional ByVal ctrlType As Long = 0, _
                            Optional ByVal scope As TreeScope = TreeScope.TreeScope_Subtree) As IUIAutomationElement
  Dim cndFirst As IUIAutomationCondition
  Dim cndSecond As IUIAutomationCondition
     
  Set cndFirst = uiAuto.CreatePropertyCondition( _
                   propertyId, _
                   propertyValue _
                 )
  If ctrlType <> 0 Then
    Set cndSecond = uiAuto.CreatePropertyCondition( _
                      UIA_ControlTypePropertyId, _
                      ctrlType _
                    )
    Set cndFirst = uiAuto.CreateAndCondition( _
                     cndFirst, _
                     cndSecond _
                   )
  End If
  Set GetElement = elmParent.FindFirst(scope, cndFirst)
End Function