Add these declarations, subroutines, and functions to a Module
Public Declare Function ShowWindow Lib "user32.dll" (ByVal Hwnd As Long, ByVal nCmdShow As Long) As Long
Public Declare Function GetWindowHandle Lib "user32" (ByVal Hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal Hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function IsWindowEnabled Lib "user32" (ByVal Hwnd As Long) As Long
Public Declare Function GetParent Lib "user32" (ByVal Hwnd As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal Hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal Hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public Const GW_HWNDNEXT As Long = 2
Public Const MAX_PATH = 260
Public Sub GetWindowInfo(Hwnd As Long, Optional sTitle As String, Optional sClass As String)
set up the strings to receive the Process Class and Window Title
sTitle = Space$(MAX_PATH)
sClass = Space$(MAX_PATH)
Run GetClassName api function and return the Process Class
Call GetClassName(Hwnd, sClass, MAX_PATH)
Run GetWindowText api function and return the Process Title
Call GetWindowText(Hwnd, sTitle, MAX_PATH)
strip the trailing chr$(0)'s from the strings returned above
sClass = TrimNull(sClass)
sTitle = TrimNull(sTitle)
Run IsWindowVisible api function to find visible windows
If IsWindowVisible(Hwnd) = 1 Then 'window visible is true
'Debug.Print "hwnd value is " & hwnd
'Debug.Print "hwnd Process Class is " & sClass
'Debug.Print "hwnd Process Title is " & sTitle
'Debug.Print "hwnd that is visible " & IsWindowVisible(hwnd)
'Debug.Print ""
End If
End Sub
Subroutine to Launch a URL using a shell command string
Public Sub MS_Edge_Launch(URL)
Create and run a Shell object to launch a separate window, Window Style = 1 for visible, Wait for return is True (wait for it to finish before executing further code)
Dim WshShell As Object
Dim CommandString As String
Set WshShell = VBA.CreateObject("WScript.Shell")
CommandString = """C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"" --start-fullscreen -new-window " & """" & URL & """"
WshShell.Run CommandString, 1, True
End Sub
Maximize Window Subroutine and Function
Public Sub InitEnumWindowsMaximizeByWindowTitle()
Debug.Print "Maximize by Window Title Initial Subroutine started"
Debug.Print "Looking for '" & WindowTitle & "' in the Window Title."
EnumWindows AddressOf EnumWindowProcMaximizeByWindowTitle, &H0
End Sub
Public Function EnumWindowProcMaximizeByWindowTitle(ByVal Hwnd As Long, ByVal lParam As Long) As Long
Dim sTitle As String
Dim sClass As String
Dim hWndProcessID As Long
GetWindowInfo Hwnd, sTitle, sClass
get the ThreadProcessID of the window
Call GetWindowThreadProcessId(Hwnd, hWndProcessID)
Find a specific window title
If sTitle Like "*" & WindowTitle & "*" Then
Debug.Print "Found '" & WindowTitle & "' In the Window Title. " & "Window hwnd = " & Hwnd & " Window Title = " & sTitle
ShowWindow Hwnd, SW_SHOWMAXIMIZED
Debug.Print "Window Maximized"
End If
'To continue enumeration, return True
'To stop enumeration return False (0).
'When 1 is returned, enumeration continues until there are no more windows left.
EnumWindowProcMaximizeByWindowTitle = 1
End Function
Put it all in to action by running this subroutine
Public Sub LaunchURL()
Dim URL As String
URL = "https://stackoverflow.com/questions/49189701/how-to-wait-until-a-specific-window-is-open"
'Call the subroutine
Call MS_Edge3.MS_Edge_Launch(URL)
'Maximize the Window
WindowTitle = "vba - How to wait until a specific"
InitEnumWindowsMaximizeByWindowTitle
End Sub