Trouble Sending emails

M

Michelle

I am using the code below, and it all seems to work fine until it gets to
the '.send' line, and then I get an 'Application or Object defined Error'.
Obviously sending is the key thing here, and I'm pulling my hair out.

I can't see anything wrong with the code unless the numeric equivalent for
olMailItem isn't 0

I really want to get this to work, it's the last peice in a big puzzle for
me.

Thanks

M

===
Sub MailSend() 'SchoolAddress As String)
Dim oLook As Object
Dim oMessage As Object

Dim mTitle As String, mBody As String

' manually set the value of olMail item for late-binding
Const olMailItem As Integer = 0

On Error Resume Next
' Get a reference to the Outlook object model
Set oLook = GetObject(, "Outlook.Application")
If Err.Number = 429 Then 'if it's not already running
Set oLook = CreateObject("Outlook.Application")
End If
On Error GoTo 0

Set oMessage = oLook.CreateItem(olMailItem)

' SchoolMail is declared elsewhere
If SchoolMail = "" Then SchoolMail = "(e-mail address removed)"
mTitle = ThisWorkbook.Sheets("Message").Range("B2").Formula
mBody = ThisWorkbook.Sheets("Message").Range("B4").Formula

' Fill and send the message
With oMessage
.To = SchoolMail
.Subject = mTitle
.Body = mBody
.Send ' it just doesn't like this line!
End With

Set oMessage = Nothing

Set oLook = Nothing

End Sub
 
N

Norman Jones

Hi Michelle,

See Ron de Bruin's suggested code at:

Mail a small message
http://www.rondebruin.nl/mail/folder3/smallmessage.htm

Adapting your data to Ron's code, try
something like:

'============>>
Public Sub MailSend()
Dim OutApp As Object
Dim OutMail As Object
Dim strSchoolMail As String
Dim strSubject As String
Dim strBody As String

Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

If strSchoolMail = vbNullString Then
strSchoolMail = "(e-mail address removed)"
End If

With ThisWorkbook.Sheets("Message")
strSubject = .Range("B2").Value
strBody = .Range("B4").Value
End With

On Error Resume Next
With OutMail
.To = strSchoolMail
.CC = ""
.BCC = ""
.Subject = strSubject
.Body = strBody
.Send 'or use .Display
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

End Sub
'<<============
 
M

Michelle

Thanks Norman - it seems that the only difference is this line
"[ApplicationObjectReference].Session.Logon" - and sure enough when I add
this line it all works fine. Do you know what this line does?

Thanks

M
 

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