• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

Excel 97 VBA, application active event

I am using Excel 97 VBA and need to determine in a module whether or not Excel is the current window that has the focus.

I have various timers running in the background and need to be able to perform some module sections but not others depending on whether Excel is the window that has the current focus or not.

I need something that equates to

if application.active="Microsft Excel" then

Any Ideas ?


1 Solution
I hope this is what you wanted. Instead of message box, you use the code that you want to perform when excel is active.

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function GetForegroundWindow Lib "user32" () As Long

Private Sub Timer1_Timer()
    Dim lngHandle As Long
    Dim strCaption As String
    strCaption = Space(50)
    lngHandle = GetForegroundWindow
    GetWindowText lngHandle, strCaption, 49
    If strCaption Like "Microsoft Excel - *" Then
        MsgBox "Excel is active"
    End If
End Sub
gnome42Author Commented:
Thankyou for your assistance it works perfectly.

The points are all yours

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now