Option Explicit
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL] VBA7 Then
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As LongPtr, ByVal wFlag As Long) As LongPtr
Private Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
Private Declare PtrSafe Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Private Declare PtrSafe Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As LongPtr, lpdwProcessId As Long) As Long
Private Declare PtrSafe Function GetForegroundWindow Lib "user32" () As LongPtr
Private Declare PtrSafe Function IsIconic 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
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL]
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL] If
Sub Start()
Application.OnTime Now, "'DisplayWindows " & True & "'"
End Sub
Sub DisplayWindows(Optional ByVal MaximizedState As Boolean = True)
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL] VBA7 Then
Dim hwnd As LongPtr
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL]
Dim hwnd As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL] If
Dim oActiveWindow As Window, oWnd As Window
ActiveWindow.Activate
Set oActiveWindow = ActiveWindow
For Each oWnd In ThisWorkbook.Windows
oWnd.Activate
If MaximizedState Then oWnd.WindowState = xlMaximized
Delay 2
Next oWnd
hwnd = FindWindow("Chrome_WidgetWin_1", vbNullString)
If hwnd Then
Call BringWindowToFront(hwnd, MaximizedState)
Delay 2
End If
VBA.AppActivate Application.Caption
oActiveWindow.Activate
MsgBox "Back to initial window." & vbCrLf & vbCrLf & "Done!", vbInformation
End Sub
Private Sub Delay(DelayTime As Single)
Dim sngTimer As Single
sngTimer = Timer
Do
DoEvents
Loop Until Timer - sngTimer >= DelayTime
End Sub
Private Sub BringWindowToFront(ByVal hwnd As LongPtr, Optional ByVal MaximizedState As Boolean = True)
Const SW_SHOW = 5
Const SW_SHOWMAXIMIZED = 3
Const SW_RESTORE = 9
Const GW_OWNER = 4
Dim lThreadID1 As Long, lThreadID2 As Long
On Error Resume Next
If hwnd <> GetForegroundWindow() Then
lThreadID1 = GetWindowThreadProcessId(GetForegroundWindow, ByVal 0&)
lThreadID2 = GetWindowThreadProcessId(hwnd, ByVal 0&)
Call AttachThreadInput(lThreadID1, lThreadID2, True)
Call SetForegroundWindow(hwnd)
If IsIconic(GetNextWindow(hwnd, 4)) Then
Call ShowWindow(GetNextWindow(hwnd, GW_OWNER), IIf(MaximizedState, SW_SHOWMAXIMIZED, SW_RESTORE))
Else
Call ShowWindow(GetNextWindow(hwnd, GW_OWNER), IIf(MaximizedState, SW_SHOWMAXIMIZED, SW_SHOW))
End If
DoEvents
Call AttachThreadInput(lThreadID2, lThreadID1, True)
End If
End Sub