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 ?


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gnome42Author Commented:
Thankyou for your assistance it works perfectly.

The points are all yours

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.