Link to home
Start Free TrialLog in
Avatar of dpokerbear
dpokerbearFlag for Australia

asked on

Emailing from MS Access

Below is the code, which was provided on http://support.microsoft.com/kb/318881

I did what the website recommended and it comes up with an error on line

   ' Add the To recipients to the e-mail message.
        Set objOutlookRecip = .Recipients.Add(TheAddress)

The error states:
 RunTime Error 287
Application-Defined or Object-Defined Error


-----------------------------------------------------------------------------------------------------------
Sub SendMessages(Optional AttachmentPath)

  Dim MyDB As Database
  Dim MyRS As Recordset
  Dim objOutlook As Outlook.Application
  Dim objOutlookMsg As Outlook.MailItem
  Dim objOutlookRecip As Outlook.Recipient
  Dim objOutlookAttach As Outlook.Attachment
  Dim TheAddress As String

  Set MyDB = CurrentDb
  Set MyRS = MyDB.OpenRecordset("tblMailingList")
  MyRS.MoveFirst

  ' Create the Outlook session.
  Set objOutlook = CreateObject("Outlook.Application")
 
  Do Until MyRS.EOF
  ' Create the e-mail message.
  Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
  TheAddress = MyRS![EmailAddress]

     With objOutlookMsg
        ' Add the To recipients to the e-mail message.
        Set objOutlookRecip = .Recipients.Add(TheAddress)
        objOutlookRecip.Type = olTo

        ' Add the Cc recipients to the e-mail message.
        If (IsNull(Forms!frmMail!CCAddress)) Then
        Else
           Set objOutlookRecip = .Recipients.Add(Forms!frmMail!CCAddress)
           objOutlookRecip.Type = olCC
        End If
   
        ' Set the Subject, the Body, and the Importance of the e-mail message.
        .Subject = Forms!frmMail!Subject
        .Body = Forms!frmMail!MainText
        .Importance = olImportanceHigh  'High importance
       
        'Add the attachment to the e-mail message.
        If Not IsMissing(AttachmentPath) Then
           Set objOutlookAttach = .Attachments.Add(AttachmentPath)
        End If

        ' Resolve the name of each Recipient.
        For Each objOutlookRecip In .Recipients
           objOutlookRecip.Resolve
           If Not objOutlookRecip.Resolve Then
             objOutlookMsg.Display
           End If
        Next
        .Send
      End With
      MyRS.MoveNext
   Loop
   Set objOutlookMsg = Nothing
   Set objOutlook = Nothing
End Sub
Avatar of Patrick Matthews
Patrick Matthews
Flag of United States of America image

When you get that error, click Debug, and then type this into the Immediate window and hit Enter:


?TheAddress


What value do you get?
If you compile the code does it compile without any errors?
(and do you have Option Explicit at the top of the module?  if not then add it and then try compiling)
this may be a silly suggestion, but does that record in the "tblMailingList" have an email address.. Just make sure that you are not assigning a null value in there... and just check the email is in correct format..
YohanF,

That was my thought as well, thus the suggestion to get the value in the Immediate window.  Could be a Null, or a zero length string, or even a concatenated list of addresses, any one of which will generate an error...

Patrick
You might like to know that you have a coding error too.
You should be checking the 'Resolved' property (past tense) not the 'Resolve' property (present tense) to confirm that the Recipient has successfully been resolved against the address book:
If Not objOutlookRecip.Resolved Then
    objOutlookMsg.Display
End If
ASKER CERTIFIED SOLUTION
Avatar of YohanF
YohanF
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh yeah, and you don't need to MoveFirst after you open your Recordset - you're already at the first record.
In fact if you don't remove this statement you'll get a run-time errorr if your tblMailingList table is ever empty.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dpokerbear

ASKER

it seems to all work fine when outlook is opened, but the error comes up only when it's closed.

i read somewhere that this may have to do with the reference library. I've added a library called Microsoft Outlook 12 Object Library. I am running Outlook 2007.

Now, this last comment from Axxede, that seems interesting. Is it safe to go passing the password and all those details through remote servers? If yes, then that might be a better option because i am making an application for multiple users, where they can set-up these parameters themselves... But anyway, that's possibly another question.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Axxede's solution is the same as I have given in that link. In your instance you wont be passing those info via remote servers as this is simply an application that is going to run on your desktop.

Jez, thank you. It works great.

Yohan, your solution might be even better for what I want, but I keep getting the error, which is attached when it tries to send.
I've specified SMTP server, but for some reason it can't connect to it. Any ideas?

Thanks.
error.JPG
What is your security settings on the SMTP server, does it allow you to connect and send emails without giving username and password.

check the port your SMTP server uses as well. Try and telnet your SMTP server from command line and send an email. If it connects to your SMTP and send an email this should work. Also you need CDO for this to work, however if you have outlook installed that should not be a problem as outlook installs CDO as well.
it needs username and password. i'll have to add those two lines in. will try it tomorrow and let you know. thanks.
It may not be an issue for you, but having the account credentials explicitly listed in your code isn't very secure.
if anyone can gain access to the code, they'll be able to establish the SMTP server username and password.
thanks