A Responsible way to Disable Ctrl+Break

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

A Responsible way to Disable Ctrl+Break

Post by MSingh »

Hi,

A few questions on the matter...

Is there is "conservative & responsible" way to disable Crt+Break ? I understand the dangerous infinte loop & to debug first.
But a user is bound to do something a developer did not anticipate.

How can one responsibly disable ^break with:

a. one code for all macros
b. a msgbox something sufficiently discouraging so that he/she does not repeat action, like "Illegal keystroke. This program will now terminate!', vbcritical,"program Shutdown" or perhaps something more forceful
c. save & Exit workbook only not quit all open workbooks?

Thanks again
Mohamed

BigKev
StarLounger
Posts: 78
Joined: 10 Feb 2010, 12:54
Location: Jeddah, Saudi Arabia

Re: A Responsible way to Disable Ctrl+Break

Post by BigKev »

Google disable control break in visual basic for applications

There are lots of good examples there.

Regards,
Kevin Bell

User avatar
HansV
Administrator
Posts: 78468
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: A Responsible way to Disable Ctrl+Break

Post by HansV »

The Application object in Word and Excel has a property EnableCancelKey that determines how Ctrl+Break is handled. See EnableCancelKey Property  [Word 2007 Developer Reference] and EnableCancelKey Property [Excel 2007 Developer Reference].
Best wishes,
Hans

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Re: A Responsible way to Disable Ctrl+Break

Post by MSingh »

Hi,
Thanks for pointing me in the right direction.
Kind Regards
Mohamed

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Re: A Responsible way to Disable Ctrl+Break

Post by MSingh »

Hi,

Following on the previous post, do I have to:
a. place code this in every procedure?
b. where a macro calls another macro then in each "submacro" also?

Sub MyProcedure()

'Dim statements

On Error GoTo HandelCancel
Application.EnableCancelKey = xlErrorHandler

‘code

HandelCancel:
If Err.Number=18 Then
If MsgBox ("Are sure you want to terminate the operation?",vbQuestion+vbYesNo,"") = vbNo Then Resume
End If
End Sub

User avatar
HansV
Administrator
Posts: 78468
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: A Responsible way to Disable Ctrl+Break

Post by HansV »

a. Yes - if you really want to do this.
b. No - when a macro calls another macro, the setting of Application.EnableCancelKey is preserved, so you don't have to set it in the "submacro". Only when ALL code has finished running does Excel reset Application.EnableCancelKey to the default xlInterrupt.
Best wishes,
Hans

MSingh
3StarLounger
Posts: 366
Joined: 12 May 2010, 06:49

Re: A Responsible way to Disable Ctrl+Break

Post by MSingh »

Thank you Hans for the quick & precise reply.

Kind Regards
Mohamed