Email - Outlook no Closing Session

N

Nigel

Hi All
I am using thse following to mail some information from Excel, whether
Outlook is currently running or not, I end up wth an instance of Outlook
that does not close. Everytime I call this it create another instance.
Why?

TIA
Cheers
Nigel

Private Sub EMail()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = (e-mail address removed)
.Subject = "Test"
.Body = "Test"
'.Attachments.Add
.DeleteAfterSubmit
On Error Resume Next
.Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
 
B

Bob Phillips

Nigel,

Try adding OutApp.Quit at the end.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

Nigel said:
Hi All
I am using thse following to mail some information from Excel, whether
Outlook is currently running or not, I end up wth an instance of Outlook
that does not close. Everytime I call this it create another instance.
Why?

TIA
Cheers
Nigel

Private Sub EMail()
Dim OutApp As Outlook.Application
Dim OutMail As Outlook.MailItem
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(olMailItem)
With OutMail
.To = (e-mail address removed)
.Subject = "Test"
.Body = "Test"
'.Attachments.Add
.DeleteAfterSubmit
On Error Resume Next
.Send
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub




----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---
 
P

Paul Robinson

Hi Nigel,
Set OutApp has created a new instance of OutLook, whether one exists
or not.
Try this. ObjoutLook is first of all set to an existing instance of
OutLook. If this fails (i.e. OutLook is not running) then an error is
generated and outLookWasRunning is set to False. If that is the case,
objOutLook is set to a new instance of OutLook.

Dim OutLookWasRunning as Boolean
On Error Resume Next
Err.Clear 'Err is Public and might have a value
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

At the end of your sub you could also put,

If Not OutlookWasRunning Then objOutlook.Quit

So that you don't leave OutLook open when it wasn't open on the users
machine.

regards
Paul
 
R

Ron de Bruin

Hi Nigel

Because there is a bug in Outlook it is possible that you must uncheck
"send immediately when connect" in the Outlook options.
<Tools>Options>Mail Setup in the Outlook menu>

Problem :
It will not close the Outlook process after the mail is sent in the Task Manager

This seems to be fixed in Office 2003
 
N

Nigel

Thanks everyone for the feedback and advice, I shall try it.

Curiously in my development environment (XP Pro / Excel 2002-SP2 / Outlook
2002 -SP2) is where I have the problem. On the target PCs - corporate
network, NT4 / Excel97 / Outlook97 the problem does not appear, but this
might be a function of the network setup?. Anyway I'll set the App to Quit
if I create an instance otherwise use the one running and leave it open.

Cheers
Nigel
 
N

Nigel

Ron,

Absolutely right, unchecking the box fixed it, the .Quit method now works OK

Cheers
Nigel
 

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