Microsoft VBA 7 - MSEdge find tab and bring to front
Attribute VB_Name = "MS_Edge"
Option Explicit
Option Compare Text
#If VBA7 Then
Private Declare PtrSafe Function EnumWindows Lib "user32" (ByVal lpEnumFunc As LongPtr, ByVal lParam As LongPtr) As Long
Private Declare PtrSafe Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As LongPtr, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare PtrSafe Function IsWindowVisible Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal nCmdShow As Long) As Long
Private Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function IsIconic Lib "user32" (ByVal hwnd As LongPtr) As Long
#Else
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function IsWindowVisible Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
#End If
Private Const SW_RESTORE As Long = 9
Private Const MAX_TITLE As Long = 255
Private gTabTitleFragment As String
Private gFoundHwnd As LongPtr
Public Function Edge_FindTabByTitle(ByVal tabTitleContains As String) As Boolean
Dim hwndTarget As LongPtr
Dim isMinimized As Long
gTabTitleFragment = tabTitleContains
gFoundHwnd = 0
#If VBA7 Then
Call EnumWindows(AddressOf EnumProc, 0)
#Else
Call EnumWindows(AddressOf EnumProc, 0)
#End If
hwndTarget = gFoundHwnd
If hwndTarget <> 0 Then
isMinimized = IsIconic(hwndTarget)
If isMinimized <> 0 Then
Call ShowWindow(hwndTarget, SW_RESTORE)
End If
Call SetForegroundWindow(hwndTarget)
Edge_FindTabByTitle = True
Else
Edge_FindTabByTitle = False
End If
End Function
Private Function EnumProc(ByVal hwnd As LongPtr, ByVal lParam As LongPtr) As Long
Dim sBuffer As String * MAX_TITLE
Dim lLength As Long
Dim sTitle As String
If IsWindowVisible(hwnd) Then ' Only consider visible windows
lLength = GetWindowText(hwnd, sBuffer, MAX_TITLE)
If lLength > 0 Then
sTitle = Left$(sBuffer, lLength)
If InStr(1, sTitle, gTabTitleFragment) > 0 Then ' Match part of the title
gFoundHwnd = hwnd
EnumProc = 0 ' Stop enumeration
Exit Function
End If
End If
End If
EnumProc = 1 ' Continue enumeration
End Function
Comments
Post a Comment