Discussion:
Outlook staying in memory
(too old to reply)
Rod
2009-06-12 07:26:29 UTC
Permalink
I am running a VBA program in Access to send emails. The email is sent OK
but Outlook stays in memory.
In the code below I have put in a message box to slow the code down. If this
is there then Outlook correctly closes itself down.
Because it is not closed it creates problems the next time I call the code,
even if it is modified to pick up an already open version of outlook.

I am using Office 2002 on a Vista machine with several cpus

Any suggestions please

many thanks


--------------------------------


Public Sub Create_eMails()

Dim olApp As New Outlook.Application
Dim olNs As Outlook.NameSpace
Dim OBmailItem As Outlook.MailItem

Set olApp = New Outlook.Application

Set olNs = olApp.GetNamespace("MAPI")
olNs.Logon ""

Set OBmailItem = olApp.CreateItem(olMailItem)

OBmailItem.To = "***@Home.com"

OBmailItem.Subject = "Test Email. "
OBmailItem.Body = "Test Body Text "
OBmailItem.Send

Call MsgBox("Wait")

olNs.Logoff

olApp.Quit

Set OBmailItem = Nothing
Set olNs = Nothing
Set olApp = Nothing

End Sub
Ken Slovak - [MVP - Outlook]
2009-06-12 13:00:39 UTC
Permalink
You need to allow time for the send operation to finish. You can try using a
DoEvents() call, but I don't think that will do it. Best thing to do
probably is to start a Send/Receive operation and wait for that to finish.
If you get a handle to SyncObjects(1) and call that SyncObject's Start()
method with an event handler for SyncEnd that should do it:

Dim WithEvents synch As Outlook.SyncObject ' in a class that can handle
events
Dim blnFinished As Boolean

When you call send you then use this:

blnFinished = False
Set synch = olNS.SyncObjects.Item(1)
synch.Start

While blnFinished = False
DoEvents
Loop

Your handler would look like this:

Private Sub synch_SyncEnd()
blnFinished = True
End Sub
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm
Post by Rod
I am running a VBA program in Access to send emails. The email is sent OK
but Outlook stays in memory.
In the code below I have put in a message box to slow the code down. If
this is there then Outlook correctly closes itself down.
Because it is not closed it creates problems the next time I call the
code, even if it is modified to pick up an already open version of
outlook.
I am using Office 2002 on a Vista machine with several cpus
Any suggestions please
many thanks
--------------------------------
Public Sub Create_eMails()
Dim olApp As New Outlook.Application
Dim olNs As Outlook.NameSpace
Dim OBmailItem As Outlook.MailItem
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
olNs.Logon ""
Set OBmailItem = olApp.CreateItem(olMailItem)
OBmailItem.Subject = "Test Email. "
OBmailItem.Body = "Test Body Text "
OBmailItem.Send
Call MsgBox("Wait")
olNs.Logoff
olApp.Quit
Set OBmailItem = Nothing
Set olNs = Nothing
Set olApp = Nothing
End Sub
Rod
2009-06-12 13:55:55 UTC
Permalink
Thanks, I give it a go, I've never played with evens before.
Post by Ken Slovak - [MVP - Outlook]
You need to allow time for the send operation to finish. You can try using
a DoEvents() call, but I don't think that will do it. Best thing to do
probably is to start a Send/Receive operation and wait for that to finish.
If you get a handle to SyncObjects(1) and call that SyncObject's Start()
Dim WithEvents synch As Outlook.SyncObject ' in a class that can handle
events
Dim blnFinished As Boolean
blnFinished = False
Set synch = olNS.SyncObjects.Item(1)
synch.Start
While blnFinished = False
DoEvents
Loop
Private Sub synch_SyncEnd()
blnFinished = True
End Sub
--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm
Post by Rod
I am running a VBA program in Access to send emails. The email is sent OK
but Outlook stays in memory.
In the code below I have put in a message box to slow the code down. If
this is there then Outlook correctly closes itself down.
Because it is not closed it creates problems the next time I call the
code, even if it is modified to pick up an already open version of
outlook.
I am using Office 2002 on a Vista machine with several cpus
Any suggestions please
many thanks
--------------------------------
Public Sub Create_eMails()
Dim olApp As New Outlook.Application
Dim olNs As Outlook.NameSpace
Dim OBmailItem As Outlook.MailItem
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
olNs.Logon ""
Set OBmailItem = olApp.CreateItem(olMailItem)
OBmailItem.Subject = "Test Email. "
OBmailItem.Body = "Test Body Text "
OBmailItem.Send
Call MsgBox("Wait")
olNs.Logoff
olApp.Quit
Set OBmailItem = Nothing
Set olNs = Nothing
Set olApp = Nothing
End Sub
Loading...