'Written: Apirl 24, 2008
'Updated: December 24, 2010 - Finds window using full or partial title
'Author: Leith Ross
'Summary: Activates an running Application using part or all of the title.
' Works with Windows 2000 to XP. Untested with Windows 2007 and up.
Option Explicit
Private Declare Function FindWindowA _
Lib "User32.dll" _
(ByVal lpszClass As String, _
ByVal lpszWindow As String) As Long
'Checks if the window is minimized to the TaskBar
Private Declare Function IsIconic _
Lib "User32.dll" _
(ByVal hWnd As Long) As Long
'Returns the handle to the window currently receiving input
Private Declare Function GetForegroundWindow _
Lib "User32.dll" () As Long
'Directs User Input to the specified Window
'Success gives a Non Zero Return Value
Public Declare Function SetForegroundWindow _
Lib "User32.dll" _
(ByVal hWnd As Long) As Long
'Bring Window to the Top of the Z-Order
Private Declare Function BringWindowToTop _
Lib "User32.dll" _
(ByVal hWnd As Long) As Long
Private Declare Function AttachThreadInput _
Lib "User32.dll" _
(ByVal idAttach As Long, _
ByVal idAttachTo As Long, _
ByVal fAttach As Long) As Long
'The return value is the identifier of the thread that created the window.
Private Declare Function GetWindowThreadProcessId _
Lib "User32.dll" _
(ByVal hWnd As Long, _
ByRef lpdwProcessID As Long) As Long
'Function to Change how Window is Displayed
Private Declare Function ShowWindow _
Lib "User32.dll" _
(ByVal hWnd As Long, _
ByVal nCmdShow As Long) As Long
'Constants for ShowWindow (nCmdShow)
Const SW_HIDDEN As Long = 0
Const SW_NORMAL As Long = 1
Const SW_MINIMIZED As Long = 2
Const SW_MAXIMIZED As Long = 3
Const SW_NOTACTIVE As Long = 4
Const SW_UNHIDDEN As Long = 5
Const SW_MINWITHFOCUS As Long = 6
Const SW_MINNOTACTIVE As Long = 7
Const SW_RESTORE As Long = 9
Private Declare Function GetWindow _
Lib "User32.dll" _
(ByVal hWnd As Long, _
ByVal wCmd As Long) As Long
Private Declare Function GetWindowText _
Lib "User32.dll" _
Alias "GetWindowTextA" _
(ByVal hWnd As Long, _
ByVal lpString As String, _
ByVal nMaxCount As Long) As Long
Private Declare Function GetDesktopWindow _
Lib "User32.dll" () As Long
Function GetHwnd(Window_Title As String) As Long
Dim hWnd As Long
Dim RetVal As Long
Dim Title As String
Const GW_HWNDNEXT As Long = 2
Const GW_CHILD As Long = 5
hWnd = GetWindow(GetDesktopWindow, GW_CHILD)
While hWnd
Title = String(512, Chr$(0))
RetVal = GetWindowText(hWnd, Title, Len(Title))
If LCase(Title) Like "*" & LCase(Window_Title) & "*" Then
GetHwnd = hWnd: Exit Function
End If
hWnd = GetWindow(hWnd, GW_HWNDNEXT)
Wend
End Function
Sub ActivateWindow(ByVal Window_Title As String)
Dim AppHwnd As Long
Dim NewPID As Long
Dim NewAppThreadID As Long
Dim RetVal As Long
Dim ThisPID As Long
Dim ThisAppThreadID As Long
Dim WndTitle As String
'Build the Window Title
WndTitle = Window_Title
'Get a handle on the Window you want to send the mouse clicks to
AppHwnd = GetHwnd(WndTitle)
'Was window found?
If AppHwnd <> 0 Then
'Application window found by the caption
ThisAppThreadID = GetWindowThreadProcessId(GetForegroundWindow, ThisPID)
NewAppThreadID = GetWindowThreadProcessId(AppHwnd, NewPID)
'Attach the other Application's thread input to this one
Call AttachThreadInput(ThisAppThreadID, NewAppThreadID, True)
'Make the other Application the top window
RetVal = SetForegroundWindow(AppHwnd)
'Detach the other Application's thread input
Call AttachThreadInput(ThisAppThreadID, NewAppThreadID, False)
If RetVal <> 0 Then
'Maximize the window if it's been minimized
'or just show it if it's already opened.
If IsIconic(AppHwnd) Then
Call ShowWindow(AppHwnd, SW_MAXIMIZED)
Else
Call ShowWindow(AppHwnd, SW_RESTORE)
End If
Else
MsgBox "Can't Bring Window to the Foreground."
End If
Else
'Failed to find the window caption
'(the app is probably closed or the wrong window name is passed)
MsgBox "Application Window '" & WndTitle & "' Not Found."
End If
End Sub