Determine If Outlook Is Running

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hello All,

I’d like to determine if Outlook 2002 is open when a procedure running
in Access, Excel, etc. attempts to send an email message so that I can
decide whether to close Outlook after the message is sent because the
user didn’t have it open or leave the instance running because the
user started it. Without examining running processes, the best I came
up with is to catch error 462 ‘The remote server machine does not
exist or is unavailable.’ after attempting to access a property of the
application object variable having set it to Outlook.Application. If
the error is thrown, I set the object variable to New
Outlook.Application and store this fact away for when it comes time
decide whether to use the Quit method of the application object. Is
there a better way to do this? To make my method more clear, the
source for a wrapper class I use to do this follows.

Thanks.

Option Explicit

Private molkApp As Outlook.Application
Private mfIsNew As Boolean

'olkApp
Public Property Get olkApp() As Outlook.Application
Set olkApp = molkApp
End Property

'Initialize
Private Sub Class_Initialize()
On Error GoTo Error

Const bytcDoingNothing As Byte = 0
Const bytcDoingCheckGet As Byte = 1

Dim bytDoing As Byte
Dim strName As String

Set molkApp = Outlook.Application

bytDoing = bytcDoingCheckGet
strName = molkApp.Name
GoTo ExitProcedure

NewApp:

bytDoing = bytcDoingNothing
Set molkApp = New Outlook.Application
mfIsNew = True

ExitProcedure:

On Error GoTo 0
Exit Sub

Error:

Select Case bytDoing
Case bytcDoingCheckGet
Select Case Err.Number
Case 462 'The remote server machine does not exist or
is unavailable.
Resume NewApp
Case Else
With Err
.Raise .Number, .Source, .Description, .HelpFile, .HelpContext
End With
End Select
Case Else
With Err
.Raise .Number, .Source, .Description, .HelpFile, .HelpContext
End With
End Select

End Sub

'Terminate
Private Sub Class_Terminate()

If mfIsNew Then
molkApp.Quit
End If

End Sub
 
Try using GetObject and if that succeeds Outlook was already running, if not
then use CreateObject or New.




Hello All,

I’d like to determine if Outlook 2002 is open when a procedure running
in Access, Excel, etc. attempts to send an email message so that I can
decide whether to close Outlook after the message is sent because the
user didn’t have it open or leave the instance running because the
user started it. Without examining running processes, the best I came
up with is to catch error 462 ‘The remote server machine does not
exist or is unavailable.’ after attempting to access a property of the
application object variable having set it to Outlook.Application. If
the error is thrown, I set the object variable to New
Outlook.Application and store this fact away for when it comes time
decide whether to use the Quit method of the application object. Is
there a better way to do this? To make my method more clear, the
source for a wrapper class I use to do this follows.

Thanks.

Option Explicit

Private molkApp As Outlook.Application
Private mfIsNew As Boolean

'olkApp
Public Property Get olkApp() As Outlook.Application
Set olkApp = molkApp
End Property

'Initialize
Private Sub Class_Initialize()
On Error GoTo Error

Const bytcDoingNothing As Byte = 0
Const bytcDoingCheckGet As Byte = 1

Dim bytDoing As Byte
Dim strName As String

Set molkApp = Outlook.Application

bytDoing = bytcDoingCheckGet
strName = molkApp.Name
GoTo ExitProcedure

NewApp:

bytDoing = bytcDoingNothing
Set molkApp = New Outlook.Application
mfIsNew = True

ExitProcedure:

On Error GoTo 0
Exit Sub

Error:

Select Case bytDoing
Case bytcDoingCheckGet
Select Case Err.Number
Case 462 'The remote server machine does not exist or
is unavailable.
Resume NewApp
Case Else
With Err
.Raise .Number, .Source, .Description, .HelpFile,
..HelpContext
End With
End Select
Case Else
With Err
.Raise .Number, .Source, .Description, .HelpFile,
..HelpContext
End With
End Select

End Sub

'Terminate
Private Sub Class_Terminate()

If mfIsNew Then
molkApp.Quit
End If

End Sub
 
Thanks for the idea. Unless I'm missing something, though, I think
what you suggest is essentially the same as what I'm doing--try to
attach to an open instance of Outlook, watch for an error from the
attempt, and if one is caught, create a new instance. GetObject is
neater in the sense that it throws an error directly without requiring
the use of an application object property.
 
That's exactly why using GetObject is the way people usually go.




Thanks for the idea. Unless I'm missing something, though, I think
what you suggest is essentially the same as what I'm doing--try to
attach to an open instance of Outlook, watch for an error from the
attempt, and if one is caught, create a new instance. GetObject is
neater in the sense that it throws an error directly without requiring
the use of an application object property.
 
Back
Top