Introduction
When you launch other applications from a VBA macro using the Shell function or manipulate applications that are already open, the target window may sometimes be hidden behind other windows.
In such cases, you can use the Windows API function SetForegroundWindow to force the specified window to the front and make it active.
This article explains the process of finding a target window using the FindWindow API and then bringing it to the foreground using the SetForegroundWindow API.
VBA Sample Code to Bring a Window to the Front
This macro checks if the Windows “Calculator” is already running. If it is, the macro brings that window to the very front.
The Declare statements must be placed at the very top of the module (before any procedures like Sub).
'--- Declare API functions at the top of the module ---
' Compatible with both 64-bit and 32-bit
#If VBA7 Then
' API function to find a window
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
' API function to bring a window to the foreground
Private Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
#Else
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
#End If
' Find the Calculator window and bring it to the front
Sub BringCalculatorToFront()
Const APP_CLASS_NAME As String = "CalcFrame" ' Window class name for Calculator
Dim windowHandle As LongPtr
'--- 1. Use FindWindow to search for the Calculator window ---
windowHandle = FindWindow(APP_CLASS_NAME, vbNullString)
'--- 2. Check the result ---
If windowHandle = 0 Then
' If handle is 0, the app is not running
MsgBox "Calculator is not running.", vbExclamation
Else
' If handle is found, bring that window to the front
SetForegroundWindow windowHandle
MsgBox "Calculator has been brought to the front."
End If
End Sub
Explanation of the Code
FindWindow API Function
To use SetForegroundWindow, you first need to know the Handle of the target window (a unique number used to identify the window). FindWindow searches for a window matching a specified class name (e.g., “CalcFrame”) or window title and returns its handle. If the window is not found, it returns 0.
SetForegroundWindow API Function
This is the core part that brings the window to the front.
- SetForegroundWindow windowHandle: By passing the
windowHandleobtained fromFindWindowas an argument, this function makes the window with that handle active and ready for user interaction.
This function is effective for restoring minimized windows or moving a window from behind others to the foreground.
Summary
In this article, I explained how to combine the Windows APIs FindWindow and SetForegroundWindow to bring a specific application window to the front.
- Use FindWindow to get the handle of the target window.
- Pass the retrieved handle to SetForegroundWindow to activate the window.
Using this technique allows you to build more advanced automation processes, such as ensuring a specific app window is visible for user confirmation during macro execution or operating multiple applications by switching between them.
