Outlook and VB question

B

Bingo

My VB6 app is referencing to Outlook Object. Once a
module/class is done, I explicitly set all objects to
nothing. But after closing the app, Outlook instance is
always left in the memory, along MAPSP32. Another issue
is when Outlook is not started, the app takes really long
to log into Outlook. In the code, I always set the new
session to True during the Logon method. I also only
pass the Namespace object ByRef. I cannot really think
about anything else to get rid of the Outlook instance.
Any recommendations will be appreciated. Thanks.
 
E

Eric Legault [MVP - Outlook]

Is this a COM Add-In? If so, you may want to review these links to ensure
that you are utlitizing best practices for releasing references:

Using Micro Eye AddInMon in an Outlook COM Add-in:
http://www.microeye.com/resources/res_addinmon.asp

Outlook COM Add-in Template:
http://www.microeye.com/resources/template.htm

This is a good example as well:

Items Command Bar:
http://www.microeye.com/resources/itemsCB.htm

Also, Outlook 2000 is notorious for not closing completely, even without
Add-Ins and fully patched.

Are you calling the Application.Quit method anywhere? You might also want
to show your code that instantiates and terminates your Outlook objects.
 
B

Bingo

Thanks for the info.

No, I'm using VB for the Outlook automation, not COM add-
in. I never used the Quit method on the Application. Is
it a good idea to call it without closing Outlook?
Thanks.

Here're the code I have

' Create objects and log on
Set m_oApp = CreateObject("Outlook.Application")
Set m_oNsp = m_oApp.GetNamespace("MAPI")
m_oNsp.Logon "", "", True, True

' Terminate objects
If Not m_oNsp Is Nothing Then
m_oNsp.Logoff
End If
Set m_oApp = Nothing
Set m_oNsp = Nothing
 
E

Eric Legault [MVP - Outlook]

Are you sure you don't have any other variables referencing
Outlook.Application? As soon as you set any of those variables to Nothing,
Outlook should terminate and remove itself from memory.

As a test, create another .exe project with this code and two buttons, one
to load Outlook and one to terminate it. If Outlook is not running when you
do this, it should terminate properly. If it is running, I'm having problems
getting Outlook to terminate as well; I need to look into this.

Option Explicit
Dim objOL As Object
Dim objNS As Object
Dim objExpl As Object
Dim objInbox As Object

Private Sub cmdLoad_Click()
On Error Resume Next

Set objOL = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then
Set objOL = CreateObject("Outlook.Application")
Set objNS = objOL.GetNamespace("MAPI")
If objOL.Explorers.Count = 0 Then
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objExpl = objOL.Explorers.Add(objInbox, olFolderDisplayNormal)

objExpl.Display

Set objInbox = Nothing
End If
End If
End Sub

Private Sub cmdQuit_Click()
If Not objExpl Is Nothing Then
objExpl.Close
End If

objOL.Quit
Set objExpl = Nothing
Set objNS = Nothing
Set objOL = Nothing
End Sub
 
E

Eric Legault [MVP - Outlook]

This updated code works for me, but only if I've disabled all of my COM
Add-Ins. Obviously, one of them isn't releasing Outlook references properly.
The same problem may be happening to you.

Option Explicit
Dim objOL As Object
Dim objNS As Object
Dim objExpl As Object
Dim objInbox As Object

Private Sub cmdExit_Click()
Unload Me
End Sub

Private Sub cmdLoad_Click()
On Error Resume Next

Set objOL = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then
Set objOL = CreateObject("Outlook.Application")
Set objNS = objOL.GetNamespace("MAPI")
If objOL.Explorers.Count = 0 Then
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objExpl = objOL.Explorers.Add(objInbox, olFolderDisplayNormal)
objExpl.Display
End If
Else
Set objNS = objOL.GetNamespace("MAPI")
End If
MsgBox "Loaded!"
End Sub

Private Sub cmdQuit_Click()
If Not objExpl Is Nothing Then
objExpl.Close
End If
Do Until objOL.Explorers.Count = 0
objOL.Explorers.Item(1).Close
Loop
objOL.Quit
objNS.Logoff
MsgBox "Quit!"
End Sub

Private Sub Form_Unload(Cancel As Integer)
Set objInbox = Nothing
Set objExpl = Nothing
Set objNS = Nothing
Set objOL = Nothing
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top