Results 1 to 3 of 3

Thread: Event to detect when Excel gets focus

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    35

    Event to detect when Excel gets focus

    Hi all,
    I’m trying to detect when Excel get focus. Well, actually I’m trying to detect when my workbook gets focus, but I want to detect when the focus is received from a different application.
    All of these methods seems to detect when I switch between Excel windows, but not other applications.


    vb Code:
    1. Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    2. Private Sub Workbook_Activate()
    3. Private Sub Workbook_WindowActivate(ByVal Wn As Window)
    4. Private Sub Application_WindowActivate(ByVal Wb As Workbook, ByVal Wn As Window)
    5. Private Sub Application _WorkbookActivate(ByVal Wb As Workbook)
    Does someone know how to do this?

    Thanks,
    -Terry

    Cross post here:
    http://www.xtremevbtalk.com/showthread.php?t=312278

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Event to detect when Excel gets focus

    those activate events only fire when a different sheet or workbook is activated in excel

    you would have to use some API functions to find if excel gets focus, then run in a timer or something similar

    check out GetActiveWindow API
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    35

    Re: Event to detect when Excel gets focus

    Thanks for the input westconn.
    I'm not sure about the effects of running a timer, but the gut feeling is that the system will be slowed down a little. The complexity also increases the chance of unexpected behaviour (due to errors or poor overall design).
    I don't think I want to go down that path for the sake of a few cosmetic features...

    -Terry

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width