I have a Excel-VBA-Macro checking technical parameters in a while-loop that ends at a fixed point in time. To stop the macro before this point in time with a keyboard-input, I tried the Application.OnKey command in vba. The problem structure is illustrated by the following code example:
Sub EndlessLoop()
Application.OnKey "{ESC}", "QuitLoop"
Do While True
' Code for the check of technical parameters
Loop
End Sub
Sub QuitLoop()
' Code for final operations before quit
End Sub
Execution of EndlessLoop keeps running endless after pressing ESC-key. Microsoft says, that OnKey ist actually not working (https://learn.microsoft.com/de-de/office/vba/api/excel.application.onkey): There is no way to currently detect the Command key in recent versions of Office VBA. Microsoft is aware of this and is looking into it.
Does anyone know a workaround for this problem?
As described, I tried some code examples with the result described above.
Task manager is no solution, because it just kills the appliction without executing the final operations before quit.
Public Declare PtrSafe Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
does not work as well and ends with an error message.