PC Review


Reply
Thread Tools Rate Thread

Activate Excel application event

 
 
Greg Lovern
Guest
Posts: n/a
 
      18th Apr 2008
Is there any way to be notified when the Excel application is
activated, after some other application was the active application?

I've enabled the Excel application-level events, but none of them fire
when the Excel application is activated.

Is there a Windows API function I can use? I looked at Appleman's
Win32 API book but didn't see one.


Any suggestions?


Thanks,

Greg
 
Reply With Quote
 
 
 
 
Peter T
Guest
Posts: n/a
 
      19th Apr 2008
You'd need to sub-class Excel's window and trap messages, a lot more than
one API but you should find examples by Dan Appleman. However, in theory
doable in VBA (I haven't attempted) but most likely to crash as soon as you
touch the VBE while the code is running!

If it's really important for you, and it's OK to have code permanently
running, you can safely report all messages to Excel's window(s) with a
C-dll and a fair amount of VBA. More details here -

http://groups.google.co.uk/group/mic...3?dmode=source

I forgot to include in that post, in the bas module I included following in
the MessageEvent proc (this is where you'd include your own code to respond
to new state of Excel's window)

Public Sub MessageEvent(arg's
Dim winState As Long, sWn As String

winState = Application.WindowState
If winState = xlMaximized Then
sWn = "Maximised"
ElseIf winState = xlMinimized Then
sWn = "Minimized"
Else
sWn = "Normal"
End If

On Error Resume Next
'If uMsg Then
Debug.Print "MSG : hWnd = " & CStr(hWnd) & "," _
; uMsg = " & GetMsgSTR(uMsg) & ", _
wParam = " & CStr(wParam) & ", _
lParam = " & CStr(lParam)," _
; "WinState = " & sWn
'End If

also, experiment to filter which messages you are interested in

Regards,
Peter T

"Greg Lovern" <(E-Mail Removed)> wrote in message
news:63bae9ac-ddcd-4391-8d5d-(E-Mail Removed)...
> Is there any way to be notified when the Excel application is
> activated, after some other application was the active application?
>
> I've enabled the Excel application-level events, but none of them fire
> when the Excel application is activated.
>
> Is there a Windows API function I can use? I looked at Appleman's
> Win32 API book but didn't see one.
>
>
> Any suggestions?
>
>
> Thanks,
>
> Greg



 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      19th Apr 2008
Not sure why I didn't think about a simple approach before. Unless you need
instantaneous notification the following might be close enough for most
purposes.

Public Declare Function GetActiveWindow Lib "user32" () As Long
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Sub StartChecking()
CheckXLOnTime True
End Sub

Sub StopChecking()
CheckXLOnTime False
End Sub

Sub CheckXLOnTime(bStart As Boolean)
Static dt As Date

If bStart Then
dt = Now + TimeSerial(0, 0, 1)
Application.OnTime dt, "CheckXLState"
ElseIf dt > 0 Then
Application.OnTime dt, "CheckXLState", Schedule:=False
dt = -1
End If

End Sub

Sub CheckXLState()
Dim bNotActive As Boolean
Dim bReCheck As Boolean
Static bNotActiveLast As Boolean

bNotActive = GetActiveWindow <> FindWindow("XLMAIN",
Application.Caption)

bReCheck = True

If bNotActiveLast <> bNotActive Then
bNotActiveLast = bNotActive

If Not bNotActive Then
' Excel activated
Call DoStuff(bReCheck)
Else
' Excel de-activated
End If

End If

If bReCheck Then CheckXLOnTime bReCheck
End Sub

Sub DoStuff(bCheck As Boolean)

bCheck = MsgBox("Hi, back again..." & vbCr & vbCr & _
"OK to continue checking or Cancel to stop", vbOKCancel) = vbOK

End Sub

I hope it's obvious what to do; place the above in a normal module, run
"StartChecking' then activate and de-activate Excel. When activated a
message should pop up within a second.

Regards,
Peter T


"Greg Lovern" <(E-Mail Removed)> wrote in message
news:63bae9ac-ddcd-4391-8d5d-(E-Mail Removed)...
> Is there any way to be notified when the Excel application is
> activated, after some other application was the active application?
>
> I've enabled the Excel application-level events, but none of them fire
> when the Excel application is activated.
>
> Is there a Windows API function I can use? I looked at Appleman's
> Win32 API book but didn't see one.
>
>
> Any suggestions?
>
>
> Thanks,
>
> Greg



 
Reply With Quote
 
Greg Lovern
Guest
Posts: n/a
 
      21st Apr 2008

Thanks, great idea.

Regarding your earlier post, we have over 25,000 users (and growing)
outside our organization using this workbook, and trying to have each
of them run a setup program for a DLL or otherwise handle a DLL is out
of the question. However, I don't need instant notification; this way
should be fine.

Thanks,

Greg


On Apr 19, 1:22 am, "Peter T" <peter_t@discussions> wrote:
> Not sure why I didn't think about a simple approach before. Unless you need
> instantaneous notification the following might be close enough for most
> purposes.
>
> Public Declare Function GetActiveWindow Lib "user32" () As Long
> Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
> ByVal lpClassName As String, ByVal lpWindowName As String) As Long
>
> Sub StartChecking()
> CheckXLOnTime True
> End Sub
>
> Sub StopChecking()
> CheckXLOnTime False
> End Sub
>
> Sub CheckXLOnTime(bStart As Boolean)
> Static dt As Date
>
> If bStart Then
> dt = Now + TimeSerial(0, 0, 1)
> Application.OnTime dt, "CheckXLState"
> ElseIf dt > 0 Then
> Application.OnTime dt, "CheckXLState", Schedule:=False
> dt = -1
> End If
>
> End Sub
>
> Sub CheckXLState()
> Dim bNotActive As Boolean
> Dim bReCheck As Boolean
> Static bNotActiveLast As Boolean
>
> bNotActive = GetActiveWindow <> FindWindow("XLMAIN",
> Application.Caption)
>
> bReCheck = True
>
> If bNotActiveLast <> bNotActive Then
> bNotActiveLast = bNotActive
>
> If Not bNotActive Then
> ' Excel activated
> Call DoStuff(bReCheck)
> Else
> ' Excel de-activated
> End If
>
> End If
>
> If bReCheck Then CheckXLOnTime bReCheck
> End Sub
>
> Sub DoStuff(bCheck As Boolean)
>
> bCheck = MsgBox("Hi, back again..." & vbCr & vbCr & _
> "OK to continue checking or Cancel to stop", vbOKCancel) = vbOK
>
> End Sub
>
> I hope it's obvious what to do; place the above in a normal module, run
> "StartChecking' then activate and de-activate Excel. When activated a
> message should pop up within a second.
>
> Regards,
> Peter T
>
> "Greg Lovern" <gr...@gregl.net> wrote in message
>
> news:63bae9ac-ddcd-4391-8d5d-(E-Mail Removed)...
>
> > Is there any way to be notified when the Excel application is
> > activated, after some other application was the active application?

>
> > I've enabled the Excel application-level events, but none of them fire
> > when the Excel application is activated.

>
> > Is there a Windows API function I can use? I looked at Appleman's
> > Win32 API book but didn't see one.

>
> > Any suggestions?

>
> > Thanks,

>
> > Greg


 
Reply With Quote
 
Peter T
Guest
Posts: n/a
 
      22nd Apr 2008
Hmm, let me think, @ 10 cents per user ..... :-)

Peter T


"Greg Lovern" <(E-Mail Removed)> wrote in message
news:3fdf97d0-5345-4b4b-b089-(E-Mail Removed)...
>
> Thanks, great idea.
>
> Regarding your earlier post, we have over 25,000 users (and growing)
> outside our organization using this workbook, and trying to have each
> of them run a setup program for a DLL or otherwise handle a DLL is out
> of the question. However, I don't need instant notification; this way
> should be fine.
>
> Thanks,
>
> Greg
>
>
> On Apr 19, 1:22 am, "Peter T" <peter_t@discussions> wrote:
> > Not sure why I didn't think about a simple approach before. Unless you

need
> > instantaneous notification the following might be close enough for most
> > purposes.
> >
> > Public Declare Function GetActiveWindow Lib "user32" () As Long
> > Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
> > ByVal lpClassName As String, ByVal lpWindowName As String) As

Long
> >
> > Sub StartChecking()
> > CheckXLOnTime True
> > End Sub
> >
> > Sub StopChecking()
> > CheckXLOnTime False
> > End Sub
> >
> > Sub CheckXLOnTime(bStart As Boolean)
> > Static dt As Date
> >
> > If bStart Then
> > dt = Now + TimeSerial(0, 0, 1)
> > Application.OnTime dt, "CheckXLState"
> > ElseIf dt > 0 Then
> > Application.OnTime dt, "CheckXLState", Schedule:=False
> > dt = -1
> > End If
> >
> > End Sub
> >
> > Sub CheckXLState()
> > Dim bNotActive As Boolean
> > Dim bReCheck As Boolean
> > Static bNotActiveLast As Boolean
> >
> > bNotActive = GetActiveWindow <> FindWindow("XLMAIN",
> > Application.Caption)
> >
> > bReCheck = True
> >
> > If bNotActiveLast <> bNotActive Then
> > bNotActiveLast = bNotActive
> >
> > If Not bNotActive Then
> > ' Excel activated
> > Call DoStuff(bReCheck)
> > Else
> > ' Excel de-activated
> > End If
> >
> > End If
> >
> > If bReCheck Then CheckXLOnTime bReCheck
> > End Sub
> >
> > Sub DoStuff(bCheck As Boolean)
> >
> > bCheck = MsgBox("Hi, back again..." & vbCr & vbCr & _
> > "OK to continue checking or Cancel to stop", vbOKCancel) =

vbOK
> >
> > End Sub
> >
> > I hope it's obvious what to do; place the above in a normal module, run
> > "StartChecking' then activate and de-activate Excel. When activated a
> > message should pop up within a second.
> >
> > Regards,
> > Peter T
> >
> > "Greg Lovern" <gr...@gregl.net> wrote in message
> >
> > news:63bae9ac-ddcd-4391-8d5d-(E-Mail Removed)...
> >
> > > Is there any way to be notified when the Excel application is
> > > activated, after some other application was the active application?

> >
> > > I've enabled the Excel application-level events, but none of them fire
> > > when the Excel application is activated.

> >
> > > Is there a Windows API function I can use? I looked at Appleman's
> > > Win32 API book but didn't see one.

> >
> > > Any suggestions?

> >
> > > Thanks,

> >
> > > Greg

>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using the VB activate event in Excel arukas03 Windows XP 0 31st Mar 2008 04:46 PM
Trigger an Excel VBA event from another application Mike Microsoft Excel Programming 4 14th Dec 2006 09:21 AM
Application Closing Event in Excel Dr. Patrick Havel Microsoft C# .NET 0 30th Nov 2006 03:05 PM
activate excel application after userform activation in modaless x taol Microsoft Excel Programming 6 29th Jan 2006 01:31 PM
Excel closing event (catch in a .net application) =?Utf-8?B?bS5haHJlbnM=?= Microsoft Dot NET 1 13th Dec 2004 06:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:04 PM.