MKONDKARI
MKONDKARI

Reputation: 9

How to verify outlook session is open or not using VBA

I have VBA Macro which extract report from LN in Excel and sent out a mail to my manager's. I have used scheduler to run this MACRO using .vbs script. This scheduler run from Monday - Friday in the morning. the problem here is we sometime have a servicing going with our system during Sat & Sunday and coz of that the outlook session get close.

What I want here is i want to write a VBA macro which will first verify that Out look session is open or not if it is open then we are fine if not we need to open the outlook session and send the mail.

Need your expertise :)

Upvotes: 0

Views: 2729

Answers (1)

Tim Williams
Tim Williams

Reputation: 166126

Try to use GetObject(,"Outlook.Application") to get a reference to a running Outlook session - if that fails then you can use CreateObject() to start Outlook

Dim olApp As Outlook.Application

On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
On Error Goto 0 

If olApp Is Nothing Then
    Set olApp = CreateObject("Outlook.Application")
End If

If olApp Is Nothing Then
    Err.Raise 999, , "Failed to get Outlook reference"
End If

Upvotes: 4

Related Questions