Declare Function GetForegroundWindow Lib "user32" () As Long
Declare Function GetDesktopWindow Lib "user32" () As Long
Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, ByRef lpdwProcessId As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Sub CheckXLWindowStatus()
If Not XLIsForegroundWindow Then
'Do timer stuff here
End If
End Sub
Function XLIsForegroundWindow() As Boolean
Dim hWndXL As Long
Dim hWndFore As Long
hWndXL = GetWindowHandle("XLMAIN", Application.Caption)
hWndFore = GetForegroundWindow
XLIsForegroundWindow = (hWndXL = hWndFore)
End Function
Function GetWindowHandle(Optional ByVal sClass As String = vbNullString, Optional ByVal sCaption As String = vbNullString)
Dim hWndDesktop As Long
Dim hwnd As Long
Dim hProcThisInstance As Long
Dim hProcWindow As Long
hWndDesktop = GetDesktopWindow
hProcThisInstance = GetCurrentProcessId
Do
hwnd = FindWindowEx(hWndDesktop, hwnd, sClass, sCaption)
GetWindowThreadProcessId hwnd, hProcWindow
Loop Until hProcWindow = hProcThisInstance Or hwnd = 0
GetWindowHandle = hwnd
End Function