how not close both Outlooks?

  • Thread starter Thread starter Ian Elliott
  • Start date Start date
I

Ian Elliott

Thanks in advance.
I have an Excel macro that opens Outlook with 'shell' and
set objOutlook=createObject, then closes it with
objOutlook.Quit.
But if I have two Outlooks open (one that was already
open, and one more that was opened by the macro), the
objOutlook.Quit closes them both. Is it possible to just
close one?
Thanks again.
 
Ian Elliott said:
Thanks in advance.
I have an Excel macro that opens Outlook with 'shell' and
set objOutlook=createObject, then closes it with
objOutlook.Quit.
But if I have two Outlooks open (one that was already
open, and one more that was opened by the macro), the
objOutlook.Quit closes them both. Is it possible to just
close one?
Thanks again.

Hi Ian,
I put this at the top of the code module where I interact with Outlook

'Declarations section
Option Explicit
Option Base 1
Public objOutlook As Outlook.Application
Public objNamespace As Outlook.NameSpace
Public OutlookWasRunning As Boolean

'Interface with Outlook objects required will be through this sub.
'It is called by any sub requiring access to Outlook.
Public Sub DeclareOutlookObjects()
Application.ScreenUpdating = False
On Error Resume Next
Err.Clear
Set objOutlook = GetObject(, "Outlook.Application") 'If
Outlook is already open, flag it with Boolean
If Err.Number <> 0 Then
OutlookWasRunning = False
Else
OutlookWasRunning = True
end if
On Error GoTo 0
Err.Clear 'keep err tidy
If Not OutlookWasRunning Then
Set objOutlook = CreateObject("Outlook.Application") 'fresh
version of Outlook
End If
Set objNamespace = objOutlook.GetNamespace("MAPI")
objNamespace.Logon 'may not be needed
End Sub

'Releases Public variable objects after Outlook session
Public Sub ReleaseOutlookObjects()
Application.ScreenUpdating = False
objNamespace.Logoff
Set objNamespace = Nothing
If Not OutlookWasRunning Then objOutlook.Quit
Set objOutlook = Nothing
End Sub

You can call the first sub before creating messages etc and the second
when you are finished. There is only ever one copy of Outlook open -
why have two?

regards
Paul
 
Back
Top