Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I use VBA to determine what window is the Active Window ? 1

Status
Not open for further replies.

vamoose

Programmer
Oct 16, 2005
320
MX
I need to be able to determine what window is the active window. Can I use Excel 2000 VBA running in the background to do this ? Thanks for the help.
 
What I am trying to do is determine if my Excel 2000 program is the active/foreground window, if it is that's great. If not, then I want to start a VBA timer set for 5 minutes then bring my Excel application back to the active/foreground when the timer expires. So I need to be able to tell which window has focus using Excel 2000 VBA.
Hope this makes sense. Thanks.
 
Hi
If I follow you this is the test you are looking for (though not tested on saved files!)

Code:
Sub a()
If ActiveWindow.Caption = ThisWorkbook.Name Then
    MsgBox "active window is this workbook"
Else
    MsgBox "active window is different workbook"
End If
End Sub

Happy Friday!

;-)
If a man says something and there are no women there to hear him, is he still wrong? [ponder]
How do I get the best answers?
 
Happy Friday Loomah!

I think vamoose is trying to determine if the Excel application window is the foreground window on the Windows Desktop. If so, here is some code that will detect that:
Code:
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

Your timer procedure should call the SetForegroundWindow API function, passing the window handle of the Excel application. I recommend changing the declaration of hWndXL to be global or alternatively, call GetWindowHandle again.


Regards,
Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top