'Written: April 18, 2009
'Author: Leith Ross
'Summary: Searchs all open Windows for the name of the workbook passed to the macro.
' If found and the workbook is a separate instance of Excel, the macro will
' activate it.
' Two sets of variables hold the window handle, process identifier, and
' thread identifier for the instance of Excel running the macro and for the
' workbook to be activated.
'
'NOTE: ALL THIS CODE MUST REMIAN IN THE SAME MODULE.
'API call Declarations
Private Declare Function EnumWindows _
Lib "user32.dll" _
(ByVal lpEnumFunc As Long, _
ByVal lparam 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
Public Declare Function SetForegroundWindow _
Lib "user32.dll" _
(ByVal hWnd As Long) As Long
Private Declare Function ShowWindow _
Lib "user32.dll" _
(ByVal hWnd As Long, _
ByVal nCmdShow As Long) As Long
Private Declare Function GetWindowThreadProcessId _
Lib "user32.dll" _
(ByVal hWnd As Long, _
ByRef lpdwProcessId As Long) As Long
Private Declare Function GetClassName _
Lib "user32.dll" _
Alias "GetClassNameA" _
(ByVal hWnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount 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
'API Variables for use in this Module Only
Private hWnd1 As Long, hWnd2 As Long
Private pid As Long, pid1 As Long, pid2 As Long
Private tid As Long, tid1 As Long, tid2 As Long
Private Workbook_Name As String
'VBA call to Activate the Workbook
Public Sub ActivateWorkbook(ByVal Wkb_Name As String)
Dim RetVal As Long
Workbook_Name = Wkb_Name
hWnd = 0: hWnd1 = 0: hWnd2 = 0
pid = 0: pid1 = 0: pid2 = 0
tid = 0: tid1 = 0: tid2 = 0
RetVal = EnumWindows(AddressOf EnumWindowProc, 0)
RetVal = AttachThreadInput(tid1, tid2, True)
RetVal = SetForegroundWindow(hWnd2)
RetVal = ShowWindow(hWnd2, 3)
RetVal = AttachThreadInput(tid1, tid2, False)
End Sub
'This function is used by ActivateWorkbook
Private Function EnumWindowProc(ByVal hWnd As Long, ByVal lparam As Long) As Long
Dim ClassName As String
Dim L As Long
Dim RetVal As Long
Dim WindowTitle As String
'Get the Window Class name
ClassName = String(512, Chr$(0))
L = GetClassName(hWnd, ClassName, 512)
ClassName = IIf(L > 0, Left(ClassName, L), "")
'Is this window an Excel Application window?
If ClassName = "XLMAIN" Then
'The Process Id is unique to each instance
tid = GetWindowThreadProcessId(hWnd, pid)
'Get the window title
WindowTitle = String(512, Chr$(0))
L = GetWindowText(hWnd, WindowTitle, 512)
WindowTitle = IIf(L > 0, Left(WindowTitle, L), "")
'Is the window title part of ThisWorkbook's title?
If InStr(1, WindowTitle, ThisWorkbook.Name, vbTextCompare) > 0 Then
hWnd1 = hWnd
tid1 = tid
pid1 = pid
End If
If InStr(1, WindowTitle, Workbook_Name, vbTextCompare) > 0 Then
'Save the window handle and its process id
hWnd2 = hWnd
tid2 = tid
pid2 = pid
EnumWindowProc = False
End If
End If
EnumWindowProc = True
End Function
Bookmarks