Linking to Outlook and sending an email

  • Thread starter Thread starter bifteki via AccessMonster.com
  • Start date Start date
B

bifteki via AccessMonster.com

I want to send an email message through Outlook. I've written the following
code.

Public Sub SendContactEmail()
Dim appOutlook As New Outlook.Application
Dim nsOutlook As NameSpace
Dim emailMsg As Outlook.MailItem
Dim sendTo As String

Set nsOutlook = appOutlook.GetNamespace("MAPI")

emailMsg.BodyFormat = olFormatHTML
emailMsg.HTMLBody = "Date:" & fld_contact_date

For Each fld_aniperson_surname In lst_contact_aniperson
emailMsg.Recipients = "SELECT fld_aniperson_id FROM tbl_anipersons
WHERE.value=tbl_anipersons.fld_aniperson_surname"
Next

emailMsg.Send
End Sub

A run-time error #91 occurs on the BodyFormat line, which says: "<Object
variable or With block variable not set>".
It' s clear that I've made a mistake somewhere or maybe the whole approach is
wrong, as I'm not very experienced in Access but I can't find how to do that.
Thanx in advance.
 
You haven't create and emailMsg object:


dim ns as object 'use late binding
dim oMessage as object 'use late binding

Set ns = oOutlook.GetNamespace("MAPI")

ns.Logon , , True, True 'Logon to exchange server

Set oMessage = oOutlook.CreateItem(0) '0=olMailItem

emailMsg.Send

Personally, I don't do use .Send, I use .Save, and
the user can send the message later.

(david)
 
david said:
You haven't create and emailMsg object:

dim ns as object 'use late binding
dim oMessage as object 'use late binding

Set ns = oOutlook.GetNamespace("MAPI")

ns.Logon , , True, True 'Logon to exchange server

Set oMessage = oOutlook.CreateItem(0) '0=olMailItem


Personally, I don't do use .Send, I use .Save, and
the user can send the message later.

(david)

There is something wrong with that code, the 'set omessage' line gives a
syntax error.
I just replaced the 0 in the parentheses with the olmailitem which if I get
it right is what you meant.
 
it right is what you meant.

Yes,

I just copied some old code, not in use, so it might
have been wrong, or the value of olMailItem might
have changed.

(david)
 
Sorry, my mistake. Instead of the omessage object I set the value of my old
variable emailMsg. The one you told me works just fine. Now I'll see if I can
make the message to be sent. Thanx for your help it was really valuable.
 
Back
Top