PDA

View Full Version : "ESC" Key press (Exit gracefully)



spencer.67965
2004-08-26, 01:45 PM
Hello all,

Just a quick question. Can anyone tell me how (or show me an example of how) I can exit gracefully out of a VBA program when the user presses the ESC key. When using the command line for user input.

Thanks,

Spencer
___________________
AutoCAD 2005 - Win 2k

Ed Jobe
2004-08-26, 02:42 PM
Put the following in your Declarations section.


'GetAsyncKeyState constants
Private Enum enGetAsyncKeyState
VK_ESCAPE = &H1B
VK_LBUTTON = &H1
VK_RBUTTON = &H2
VK_RETURN = &HD
VK_SPACE = &H20
End Enum

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As enGetAsyncKeyState) As Integer


Then add the following error handler to your procedure:


Exit_Here:
ThisDrawing.EndUndoMark
Exit Sub

Err_Control:
Select Case Err.Number
Case -2147352567
If GetAsyncKeyState(VK_ESCAPE) And &H8000 > 0 Then
Err.Clear
Resume Exit_Here
ElseIf GetAsyncKeyState(VK_LBUTTON) > 0 Then
Err.Clear
Resume
End If
Case Else
MsgBox Err.Number & " - " & Err.Description, vbCritical, "YourProcedureName"
Resume Exit_Here
End Select
End Sub

warren.medernach
2004-08-26, 02:44 PM
Hello Spencer.
Something like this should do the trick:


Sub test()

Dim dblDist As Double

On Error GoTo ErrorHandler

Do

dblDist = ThisDrawing.Utility.GetDistance(, "Enter Distance: ")

Loop While Not IsEmpty(dblDist)


ErrorHandler:
If Err.Number = -2147352567 Then
Exit Sub
End If

End Sub


Hope this helps

Warren M



Hello all,

Just a quick question. Can anyone tell me how (or show me an example of how) I can exit gracefully out of a VBA program when the user presses the ESC key. When using the command line for user input.

Thanks,

Spencer
___________________
AutoCAD 2005 - Win 2k

spencer.67965
2004-08-27, 02:59 PM
Thanks for the input.


Spencer