How to Test if Outlook is Open or not?

Are you automating any task with Ms Outlook App?

Then, sometimes, you need to verify that the Outlook is already running in machine or not.

Here is the code… But wait…

Read the important note below the code.

Sub Is_Outlook_Running_Open_App()
    'Declare Variables to Check Get Instance of Outlook Object
    Dim objOutlook As Object
    
    'Initialize
    Set objOutlook = Nothing
    
    'Get Instance of Object
    'Getobject will give error if it did not find the app.
    'Sp, On Error is required all the time
    On Error Resume Next
    Set objOutlook = GetObject(, "Outlook.Application")
    On Error GoTo 0
    
    'Check if Outlook is Running
    If objOutlook Is Nothing Then
        'Outlook is not Running - Open Outlook App
        VBA.Shell ("Outlook")
    Else
        MsgBox "Outlook is already Running in Your Machine"
    End If
End Sub

In the above code, the Vba.Shell command will open the Outlook Application if it is not already running.

Note: To explicitly open Outlook send/receive use this line in your vba code. ObjOutlook.Application.GetNamespace(“MAPI”).SendAndReceive (True)

Once this is executed, you can see that “Send/Receive” window is opened & Outlook will start processing the emails in outbox.

Outlook running or not – Why the check is performed?

When you are sending any automatic mass/bulk marketing emails using Vba, then Outlook will start sending the emails only when it is running. It is not enough to create an object.

For this purpose, we check if it is already running or not & then act accordingly.

Usually developers use late-binding for initiating an object with Outlook.Application. There is not much of a need to check whether it is already running or not.

If you are using early binding for Outlook App, then include reference to “Microsoft Outlook Library” from Tools -> References. Many times, this will be a problem, if the destination machine is having a lower version of Office than that you are coding.

So, it is better to use late-binding for Office Apps like Outlook & Word.

Leave a Reply