O
OssieMac
Office XP (2002) Professional
I am having problems with sending emails from Excel 2002 via Outlook. After
the code has run a number of times then there are multiple processes of
OUTLOOK.EXE in Task Manager. This occurs when Outlook is not already open. If
Outlook is already open and GetObject works then no problem. However, if not
open and uses CreateObject then it works for a while then eventually brings
the system down.
Below are 2 demo Subs. The first is the problem code and then below that is
my workaround that works fine because it requires Outlook to be already open.
However, I would like to know if perhaps my first sub can be improved to
overcome my problem.
Sub SendEmail_1()
'This code creates multiple processes of OUTLOOK.EXE in
'Task Manager if Outlook is not already open.
Dim objOutlook As Outlook.Application
Dim objMail As Outlook.MailItem
Dim bolCreate As Boolean
On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
If Err.Number > 0 Then
On Error GoTo 0
Set objOutlook = CreateObject("Outlook.Application")
bolCreate = True
End If
On Error GoTo 0
Set objMail = objOutlook.CreateItem(olMailItem)
With objMail
.To = "(e-mail address removed)"
.CC = ""
.BCC = ""
.Subject = "My Test email"
.Body = "Test message only"
.Send
End With
Set objMail = Nothing
If bolCreate Then
objOutlook.Quit 'Only quit if previously not open.
End If
Set objOutlook = Nothing
End Sub
'********************************
Sub SendEmail_2()
'This code works fine but it requires Outlook to be already open.
Dim objOutlook As Outlook.Application
Dim objMail As Outlook.MailItem
On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
If Err.Number > 0 Then
On Error GoTo 0
MsgBox "Outlook must be open before you can send emails." _
& vbCrLf & vbCrLf & _
"Open Outlook then re-run the code."
Exit Sub
End If
On Error GoTo 0
Set objMail = objOutlook.CreateItem(olMailItem)
With objMail
.To = "(e-mail address removed)"
.CC = ""
.BCC = ""
.Subject = "My Test email"
.Body = "Test message only"
.Send
End With
Set objMail = Nothing
Set objOutlook = Nothing
End Sub
I am having problems with sending emails from Excel 2002 via Outlook. After
the code has run a number of times then there are multiple processes of
OUTLOOK.EXE in Task Manager. This occurs when Outlook is not already open. If
Outlook is already open and GetObject works then no problem. However, if not
open and uses CreateObject then it works for a while then eventually brings
the system down.
Below are 2 demo Subs. The first is the problem code and then below that is
my workaround that works fine because it requires Outlook to be already open.
However, I would like to know if perhaps my first sub can be improved to
overcome my problem.
Sub SendEmail_1()
'This code creates multiple processes of OUTLOOK.EXE in
'Task Manager if Outlook is not already open.
Dim objOutlook As Outlook.Application
Dim objMail As Outlook.MailItem
Dim bolCreate As Boolean
On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
If Err.Number > 0 Then
On Error GoTo 0
Set objOutlook = CreateObject("Outlook.Application")
bolCreate = True
End If
On Error GoTo 0
Set objMail = objOutlook.CreateItem(olMailItem)
With objMail
.To = "(e-mail address removed)"
.CC = ""
.BCC = ""
.Subject = "My Test email"
.Body = "Test message only"
.Send
End With
Set objMail = Nothing
If bolCreate Then
objOutlook.Quit 'Only quit if previously not open.
End If
Set objOutlook = Nothing
End Sub
'********************************
Sub SendEmail_2()
'This code works fine but it requires Outlook to be already open.
Dim objOutlook As Outlook.Application
Dim objMail As Outlook.MailItem
On Error Resume Next
Set objOutlook = GetObject(, "Outlook.Application")
If Err.Number > 0 Then
On Error GoTo 0
MsgBox "Outlook must be open before you can send emails." _
& vbCrLf & vbCrLf & _
"Open Outlook then re-run the code."
Exit Sub
End If
On Error GoTo 0
Set objMail = objOutlook.CreateItem(olMailItem)
With objMail
.To = "(e-mail address removed)"
.CC = ""
.BCC = ""
.Subject = "My Test email"
.Body = "Test message only"
.Send
End With
Set objMail = Nothing
Set objOutlook = Nothing
End Sub