End Main Sub on vbCancel within called Subroutine

ascalise

New Member
Joined
Oct 8, 2015
Messages
21
As title says, I have a main sub that calls a subroutine that displays a Retry/Cancel Msgbox. On cancel, I want to Exit the subroutine AND not return to the main sub. Currently I have an Exit Sub on cancel, but it only quits the subroutine and continues on the next line of the main sub. Do I need to make this a function and pass a bool back to tell the main sub that it too should stop executing? Here's the snipped piece of my subroutine:

If Result = vbCancel Then Exit Sub
Else
On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
On Error GoTo 0
End If
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
That's one way. You can also raise an unhandled error that will then be passed back up to the main sub where it can be dealt with appropriately, even if that's just by exiting.
 
Upvote 0
That's one way. You can also raise an unhandled error that will then be passed back up to the main sub where it can be dealt with appropriately, even if that's just by exiting.

You may want to use 'End' instead of 'Exit Sub'
Please be cautious if you have open forms, or whatever that it would be good to close prior to bugging out.
https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/end-statement

Thank you both for your help. I was thinking of passing the error back to the main sub but doesn't the subroutine need to not have an existing handler in order to do that? I already have a handler in that sub to allow the retry loop to work (checking if Outlook is open). Hence why I looked into passing a value through a function instead.

I first tried an End Sub within the vbCancel condition, but you can't have an End Sub in the middle of a subroutine. However, just looking into it deeper, End alone seems to end all procedures and clear global variables. Will use this for now and see how things go. Thanks again for the ideas.
 
Last edited:
Upvote 0
I can't see any reason you would need an active error handler at that point, but even with one you can simply raise another error in the error handler. End is a little brute force for my taste.
 
Upvote 0
I can't see any reason you would need an active error handler at that point, but even with one you can simply raise another error in the error handler. End is a little brute force for my taste.

I currently don't have an active handler. My current code is currently exiting the handler with GoTo 0 statements, looping GetObject until the Outlook object is not nothing [Set oOutlook = GetObject(, "Outlook.Application")]. Are you suggesting that instead of using End I purposely raise an error and pick it up with a handler with an End Sub?

Sub IsOutlookOpen()
Dim oOutlook As Object


On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
On Error GoTo 0


Do While oOutlook Is Nothing
Result = MsgBox("Outlook not open, cannot send update email.", vbRetryCancel + vbExclamation)
If Result = vbCancel
End
Else
On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
On Error GoTo 0
End If
Loop
End Sub
 
Upvote 0
Just raise an error there instead of using End. It will cause execution to go back to the main sub where you can handle the error.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top