Solved

Excel VBA detect change to non-Excel window

Posted on 2014-03-16
3
542 Views
Last Modified: 2014-03-18
Is there a way to use Excel VBA to detect when the user changes to another application window ( not an Excel window )?  

The Excel app may or may not have a userform active at the time of the switch.  

I have tried the workbook_deactivate event and found that it seems to fire only for a change to another workbook but not to another application.  

This is code I have tried -

Private Sub Workbook_WindowDeactivate(ByVal Wn As Excel.Window)

  Dim f, fc, fl, fs, count, NewFilePath
 
  'check for existence of file in folder LatestTestfile
  Set fs = CreateObject("Scripting.FileSystemObject")
  NewFilePath = ThisWorkbook.Path & "\LatestTestFile"
  Set f = fs.GetFolder(NewFilePath)
  Set fc = f.Files
  count = 0
  For Each f1 In fc
    count = count + 1
  Next
  If (count > 0) Then
    If (InStr(1, ThisWorkbook.Name, "console") > 0) Then
      MsgBox "A test has not been logged.  Complete the log entry before leaving the console window"
      Wn.Activate
    End If
  End If
 
End Sub
0
Comment
Question by:crm-serv
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
regmigrant earned 500 total points
ID: 39932736
You would need to have a hook into the Windows level event handler (ie: outside excel). I believe this is only possible with a DLL to handle it for you however the following link points to a possible open source solution which is intended for VB but may provide a way forward for you:-

http://www.vbaccelerator.com/home/index.asp
http://www.vbaccelerator.com/home/VB/Code/Libraries/Hooks/vbAccelerator_Hook_Library/article.asp
0
 

Author Comment

by:crm-serv
ID: 39932768
Thank you.  I'll take a look.
0
 

Author Closing Comment

by:crm-serv
ID: 39937141
I was hopeful of a more direct approach but realize now that is not available.

I appreciate you pointing me in the right direction and your prompt response.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question