Call a Sub Multiple Times Due to Error Handler

wsnyder

Board Regular
Joined
Sep 23, 2018
Messages
223
Office Version
  1. 365
Platform
  1. Windows
Hi all,

Is it ok to call my sub multiple times as I am doing below?
Is there a better way to make sure the sub executes?

Thanks,
-w

VBA Code:
Sub foo()


    Dim olApp As Object
    
    Call ShutdownExcelEvironment
    
    On Error Resume Next
    Set olApp = CreateObject("Outlook.Application")
    
    If Not olApp Is Nothing Then
        'Object exists continue
    Else
        MsgBox "The Outlook Application does not exist"
        Debug.Print "reached"
        Call RestoreExcelEvironment
        Exit Sub
    End If
    
    Debug.Print "ta-da!"
    Call RestoreExcelEvironment
    
End Sub



Public Sub ShutdownExcelEvironment()
    With Application
      .Calculation = xlCalculationManual
      .ScreenUpdating = False
      .DisplayStatusBar = False
      .EnableEvents = False
      .DisplayAlerts = False
    End With
End Sub


Public Sub RestoreExcelEvironment()
    With Application
      .Calculation = xlCalculationAutomatic
      .ScreenUpdating = True
      .DisplayStatusBar = True
      .EnableEvents = True
      .DisplayAlerts = True
    End With
End Sub
 

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
I personally do not see anything wrong with what you are doing. I would do it a bit differently, but that mainly boils down to preferences.

VBA Code:
    If Not olApp Is Nothing Then
       Debug.Print "ta-da!"
        'Object exists continue
    Else
        MsgBox "The Outlook Application does not exist"
        Debug.Print "reached"
    End If
 
    RestoreExcelEvironment

Doing the above would achieve the same outcome, if I am reading it correctly.

--EDIT--
Something that I would add is the inclusion of 'On Error goto 0'.
When you use 'On Error Resume Next' you are basically telling Excel, "Yeah, I know this is going to fail. I want it to. Just skip it." Once you are finished with the 'errored' part, you need to add in 'On Error goto 0'. That will reset Excel back to its default error catching functions.
 
Upvote 0
Thanks, frabulator,

Appreciate your thoughts/insights.

Thanks,
-w
 
Upvote 0

Forum statistics

Threads
1,215,073
Messages
6,122,976
Members
449,095
Latest member
Mr Hughes

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