views:

671

answers:

2

Hello!

I am currently writing a vba macro to send e-mails and the messages are created but do not sent as an error is generated. My current code is:

Function CreateHURMail(Filename)

Dim olApp As Outlook.Application
Dim objMail As Outlook.MailItem
Set olApp = New Outlook.Application
Set objMail = olApp.CreateItem(olMailItem)

With objMail
    .Subject = "Test Message"
    .Body = "Body Text"
    .To = "abc@xyz"
    .Attachments.Add (Filename)
    .Display

    On Error Resume Next
    .Send

    'If Err.Number = 287 Then
    '    MsgBox "Still doesn't work!", vbOKOnly, "DOH!"
    'End If
End With


End Function

Does anyone know how to fix this?

Thanks in advance.

+1  A: 

Hi,

In Access use DoCmd.SendObject to send an e-mail. Example:

Call DoCmd.SendObject(acSendNoObject, To:="abc@xyz", 
    Subject:="Test Message", MessageText:="Body Text", EditMessage:=true)

In stead of sending No Object, you can also send tables, queries, forms, reports or forms. It isn't possible to attach a normal file, this way.

If you automate Outlook and try to send a message, it is caught by Outlook. Depending on Outlook's security setting, it disallows sending mail through automation completely, it asks the user using a popup if automation is allowed or it simply sends the mail (be careful with the latter).

If sending the mail is aborted either because the security disallows sending mail via automation completely or because the user clicked "no" on the confirmation dialog box, error 287 occurs.

There are two ways to resolve it: disable security (completely or let the user confirm sending the mail), or sign your mdb-file and trust it in Outlook. The latter is rather complicated, but the most secure.

Hope this helps,

Guillaume Hanique

Guillaume Hanique
A: 

You may wish to consider Outlook Redemption

Remou