Excel COM dll - how to escape from process

M

mac_in_sctland

Hi,

I have built a fairly simple Excel COM addin in visual basic 6 which
loop 50000 times and adds up numbers.

The problem I am having is that when I press the escpae key the COM
addin doesn't stop (I would have expected it to - the same as VBA
macros).

Is there anything I need to do to put into my COM addin to allow this
to happen?

Many thanks for your help

Mac
 
P

Peter T

I recall struggling with that one!
This is what I eventually came up with for use in a VB6 dll -


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


Function IsKeyDown(key As Long) As Boolean
If GetAsyncKeyState(key) Then
IsKeyDown = True
End If

End Function

Function EscBreak() As Long

If IsKeyDown(vbKeyCancel) Then
EscBreak = 8218 ' ctrl-break
ElseIf IsKeyDown(vbKeyPause) Then
EscBreak = 8218 ' simply Break
ElseIf IsKeyDown(vbKeyEscape) Then
EscBreak = 8219 ' Esc
End If

End Function

Sub test()
Dim i As Long
Dim x As Double
Dim nextKeyCheck As Long
Dim nKey As Long
Const cLOOPS As Long = 20000 ' adjust

' adjust cLOOPS to trigger every say 0.1 to 0.2 seconds

' EnableCancelKey in VBA only for testing
' don't include EnableCancelKey in VB6

Application.EnableCancelKey = xlDisabled
On Error GoTo errH
For i = 1 To 100000000
x = x + 0.01

If i > nextKeyCheck Then
nextKeyCheck = nextKeyCheck + cLOOPS
nKey = EscBreak
If nKey Then
Err.Raise 12345
nKey = 0
End If
End If

Next
i = i - 1

done:
Debug.Print i, x ' note floating point error!!

Application.EnableCancelKey = xlInterrupt

Exit Sub

errH:

If Err.Number = 12345 Then
If MsgBox("You pressed " & IIf(nKey = 8218, "Break", "Esc") & _
" in loop " & i & vbCr & _
"Do you want to continue", vbYesNo) = vbYes Then
Resume Next
Else
' do cleanup stuff
Resume done
End If

Else
' some other error
End If

End Sub


Regards,
Peter T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top