+ Reply to Thread
Results 1 to 2 of 2

Creating Application Activate Event

Hybrid View

duGly Creating Application Activate... 05-15-2015, 12:19 PM
Leith Ross Re: Creating Application... 05-15-2015, 02:12 PM
  1. #1
    Registered User
    Join Date
    09-13-2010
    Location
    Utah, USA
    MS-Off Ver
    Excel 2003
    Posts
    3

    Creating Application Activate Event

    I am using VBA with Excel 2010. I am familiar with creating event handling procedures at the worksheet and workbook levels (as well as smaller objects such as UserForm or various controls). I want to capture an Activate event when the user switches to another application, and then comes back (activates) Excel. The worksheet activate event does not fire, because I'm not changing the active sheet (nor do I want to). Nor does the workbook activate event, because I'm not changing active workbooks.

    I have created a class module to try capture the event. It works when I first open Excel, but then fails to run when I leave and come back to Excel. Here is the code:

    ' ••cls_mdlExcelEvents Class Module••
    Private WithEvents App As Application
    Option Explicit
    
    Private Sub Class_Initialize()
        Set App = Application
    End Sub
    
    Private Sub App_WindowActivate(ByVal Wb As Workbook, ByVal Wn As Window)
        MsgBox "It Works!", vbOKOnly, "Test"
    End Sub
    Here is the reference to my class module from my ThisWorkbook module:

    ' ••ThisWorkbook Module••
    Private xlApp As cls_mdlExcelEvents
    Option Explicit
    
    ' •Workbook_Open Event Handler•
    Private Sub Workbook_Open()
        Set xlApp = New cls_mdlExcelEvents
    End Sub

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Creating Application Activate Event

    Hello duGly,

    Activation has different meanings depending on the context. In VBA the application activate event is fired when the application is first loaded and started. The events you are trying capture are LostFocus and GotFocus. When the window that is receiving user input switches, the Windows message "WM_ACTIVATE" is sent to both windows. Messages are handled by the WndProc (Window Procedure) for each window. The low order word of the message is checked by each WndProc. If it is a 0 (zero) then that window is deactivated (no longer receives input). If it is a 1 or 2 then the window receiving the message will be activated (Receive input and be brought to the top of all other windows).

    Unfortunately, the events are not exposed through the VBA interface. Using the Windows API, you can intercept and process messages sent or posted to a particular window before the window has a chance to process them. This technique is known as Subclassing. By Subclassing a window, an application can augment, modify, or monitor the behavior of the window. An application subclasses a window by replacing the address of the window's original window procedure with the address of a new window procedure, called the subclass procedure. Thereafter, the subclass procedure receives any messages sent or posted to the window.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Deactivate event appears to be overriding next activate event
    By ezrizer in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 06-01-2013, 10:15 AM
  2. VBE activate event
    By digita in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-28-2007, 10:47 PM
  3. Workbook Activate Event
    By Chris in forum Excel General
    Replies: 5
    Last Post: 07-26-2006, 01:30 PM
  4. [SOLVED] On activate event
    By SHIPP in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-30-2005, 12:45 PM
  5. Creating An Application Event Handler
    By HLong in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-30-2005, 05:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1