+ Reply to Thread
Results 1 to 5 of 5

Test if Application has focus

  1. #1
    Forum Contributor
    Join Date
    11-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    405

    Question Test if Application has focus

    Hi Guys,

    I'm looking to gain a better understanding of user usage and behavior in my excel/SQL application.

    One aspect of this that I am interested in is the session time of the user - in particular, I wish to use a timer to count the amount of time the application is open, AND, has focus.

    That is - when the excel window is maximized and is not behind windows of other applications.

    I have the following timer code, but I need your help with testing for the conditions outlined above.

    Please Login or Register  to view this content.
    Your help on this is greatly appreciated,

    Regards
    Jordan
    Last edited by jordan2322; 10-14-2014 at 06:54 PM.

  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,248

    Re: Test if Application has focus

    Hello Jordan,

    This is a unique and interesting idea. To do this requires using the Windows API.

    Copy the macro code below into a separate VBA Module in your Workbook. You call then call it your code when needed.

    The macro compares the application window handle to the active window handle. If they match then Excel has the focus and the macro returns TRUE.

    Please Login or Register  to view this content.
    Here is an example of calling the macro in your code...
    Please Login or Register  to view this content.
    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!)

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

    Re: Test if Application has focus

    Hello Jordan,

    This is a unique and interesting idea. To do this requires using the Windows API.

    Copy the macro code below into a separate VBA Module in your Workbook. You call then call it your code when needed.

    The macro compares the application window handle to the active window handle. If they match then Excel has the focus and the macro returns TRUE.

    Please Login or Register  to view this content.
    Here is an example of calling the macro in your code...
    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    11-16-2011
    Location
    Australia
    MS-Off Ver
    Excel 2010
    Posts
    405

    Re: Test if Application has focus

    Thank you for your response Leith,
    This is working on a Windows XP machine (32bit), but I am also looking to deploy this across Windows 7 64 bit machines, how will this effect the above code you have provided?
    Cheers, Jordan

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

    Re: Test if Application has focus

    Hello Jordan,

    You can use this macro as is on 64 bit machines that are running a 32 bit versions of Office. My machine is 64 bit running Windows 7 with a 32 bit version of Office 14.0.

    If the macro is to run under a 64 bit version of Office then you will need to use this code.

    NOTE: You can also run this code on 64 bit machines that are running a 32 bit version of Office with no changes. However, it will fail if run on a 32 bit machine running 32 bit Office.

    Macro Code for 64 bit Office
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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