Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kinuasa/c28b76bc60122b1a2cd2ba63c5459796 to your computer and use it in GitHub Desktop.
Save kinuasa/c28b76bc60122b1a2cd2ba63c5459796 to your computer and use it in GitHub Desktop.
ページのタイトルを指定してEdgeのタブを閉じるVBAマクロ 関連Q&A:https://docs.microsoft.com/en-us/answers/questions/566091/how-to-close-microsoft-edge-browser-coding-by-exce.html
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment