SolidWorks API - Macro Interrupt via Escape Key

Hi all

I wish I could display a MsgBox if the user presses the "ESCAPE" key while my macro is running.

I added at the beginning of my macro:

     Excel.Application.OnKey "{ESC}", "Escape"

And set the MsgBox in the following Sub:

Sub Escape()
        If MsgBox("Stop macro?", vbYesNo + vbQuestion) = vbYes Then
            Exit Sub
        Else
            Summary  'Resumes execution
        End If
End Sub

 

But it doesn't work. Does anyone have an idea?

Thank you in advance.

1 Like

Hello

Are you on SW or Excel?

Isn't it a bit dangerous to be able to stop the code at any time? You only read on your SW files I hope?

See you...

1 Like

Hello

If the code is not launched, nothing happens. I tested with just Application.OnKey and it works great.

On the other hand, this function must be active at all times, so it should probably be placed in the sheet where the control of the user's actions should be done.

1 Like

@Remrem

I'm on SW but Application.OnKey is part of the Excel Library. I don't just read but my macro communicates with our ERP via WebServices...

@Cyril.f

Does it work from a SW or Excel macro?

1 Like

Excel, not tested on SW.

In my opinion it will not be possible to use excel procedures in SW. After a quick search, look at the vbkey level that applies to the forums.

Thank you Cyril.

It works as desired.

Sub Control_KeyDown()
   
    DoEvents
    If (GetAsyncKeyState(vbKeyEscape)) Then
        If MsgBox("Stop macro?", vbYesNo + vbQuestion) = vbYes Then
            End
        Else
        End If
    End If
        
End Sub

You're welcome.