Link to home
Start Free TrialLog in
Avatar of jana
janaFlag for United States of America

asked on

How to know via code when a user presses the windows X' or the ESC key in a VBA apps

We are trying to determine how to capture in the VBA code (specifically in Outlook VBA) when a user clicks the "X" button of the UserForm window and also if the user presses the ESCAPE key on the keyboard.
ASKER CERTIFIED SOLUTION
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jana

ASKER

That helped on when the user clicks on the "X" exit button of the form, but what about when the user presses the ESCape keyboard?
Avatar of jana

ASKER

We have a UserForm that does a drop-down combobox window.

We have a Boolean variable named 'OptionNotSelected' that we want to set to TRUE when the user either press the ESCape keyboard or clicks on the "X" of the UserForm to exit.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jana

ASKER

doesn't work
Please show the exact code you are trying, and explain what you mean by "doesn't work".
Avatar of jana

ASKER

I just copied your exact code.  Attached is an example in a an Excel (we have to do this for both an Excel and Outlook).  


TestEsc-Xbutton.xlsm
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jana

ASKER

Ok, you initial script was placing it in the UserForm, we have to place it in the field area not the form.

How about when clicking on the "X" corner ?

How doe we use your script:


   If CloseMode <> 1 Then
        Cancel = True
        MsgBox "The Close box has been disabled", vbExclamation, "No soup for you!"
    End If
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jana

ASKER

I placed it in the UsertForm1 and it didnt work..  Do i have to put it in the fields (TextBox)?
Avatar of jana

ASKER

It did worked!

Thanx!!
escape won't work in the form itself but the close will as it is not in the text box where the 'textbox' has the focus all the escape key does is clear the box
Avatar of jana

ASKER

Dont understand, I tried it and it works, ESC key when pressed is being captured.
Avatar of jana

ASKER

Thanx