Error 287 - Access 2007 with Outlook

  • Thread starter Thread starter troy23
  • Start date Start date
T

troy23

I have my references set to Outlook 2007 within Access 2007.

When I try to send mail from Access 2007 it bombs on the statement

With .Recipients.Add(Me!txtTo)

The error number is 287.

I have searched all over the net and nobody seems to know what the
solution is.

Any ideas?
 
I don't believe the Add method of the Recipients collection returns an
object, which is what you'd need in order to be able to use the With
keyword. What's the rest of your code look like?
 
Hi

Thanks for taking the time to look at this.

It's always worked fine in previous versions of Access, but not in
2007 for some reason.

My code is as follows.

Private Sub NewMailMessage()
'Create a new mail message
Dim itmMail As Outlook.MailItem


On Error GoTo NewMailMessage_Error

'Return a reference to the MAPI layer
Set nsMAPI = olApp.GetNamespace("MAPI")

'Create a new mail message item
Set itmMail = olApp.CreateItem(olMailItem)
With itmMail
'Add the subject of the mail message

If Not NoData(Me!txtTo) Then
.Subject = Me!txtTo
Else
MsgBox "You must enter a recipient", vbOKOnly, "Outlook"
Me!txtTo.SetFocus
Exit Sub
End If

If Not NoData(Me!txtSubject) Then
.Subject = Me!txtSubject
Else
MsgBox "You must enter a subject", vbOKOnly, "Outlook"
Me!txtSubject.SetFocus
Exit Sub
End If

'

If Not NoData(Me!txtAdditionalMess) Then
.Body = Me!txtAdditionalMess
Else
End If

'Add a recipient and test to amke sure that the
'address is valid using the Resolve method

With .Recipients.Add(Me!txtTo)
.Type = olTo
If Not .Resolve Then
MsgBox "Unable to resolve address.", vbInformation
Exit Sub
End If
End With



'Send the mail message

.Send

MsgBox "Your message was sent successfully", vbOKOnly, "Outlook"
End With

'Release memory
Set itmMail = Nothing
Set nsMAPI = Nothing
Set olApp = Nothing
Exit Sub

NewMailMessage_Error:
Error_Handler ("NewMailMessage")
Exit Sub

End Sub
 
Hmm. On doing a bit more research, it looks as though I was wrong, and the
line of code should work.

See whether changing

With .Recipients.Add(Me!txtTo)
.Type = olTo
If Not .Resolve Then
MsgBox "Unable to resolve address.", vbInformation
Exit Sub
End If
End With

to

Dim rcpMail As Outlook.Recipient

Set rcpMail = .Recipients.Add(Me!txtTo)
Set rcpMail.Type = olTo
If Not rcpMail.Resolve Then
MsgBox "Unable to resolve address.", vbInformation
Exit Sub
End If

works.

--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Hi

Thanks for taking the time to look at this.

It's always worked fine in previous versions of Access, but not in
2007 for some reason.

My code is as follows.

Private Sub NewMailMessage()
'Create a new mail message
Dim itmMail As Outlook.MailItem


On Error GoTo NewMailMessage_Error

'Return a reference to the MAPI layer
Set nsMAPI = olApp.GetNamespace("MAPI")

'Create a new mail message item
Set itmMail = olApp.CreateItem(olMailItem)
With itmMail
'Add the subject of the mail message

If Not NoData(Me!txtTo) Then
.Subject = Me!txtTo
Else
MsgBox "You must enter a recipient", vbOKOnly, "Outlook"
Me!txtTo.SetFocus
Exit Sub
End If

If Not NoData(Me!txtSubject) Then
.Subject = Me!txtSubject
Else
MsgBox "You must enter a subject", vbOKOnly, "Outlook"
Me!txtSubject.SetFocus
Exit Sub
End If

'

If Not NoData(Me!txtAdditionalMess) Then
.Body = Me!txtAdditionalMess
Else
End If

'Add a recipient and test to amke sure that the
'address is valid using the Resolve method

With .Recipients.Add(Me!txtTo)
.Type = olTo
If Not .Resolve Then
MsgBox "Unable to resolve address.", vbInformation
Exit Sub
End If
End With



'Send the mail message

..Send

MsgBox "Your message was sent successfully", vbOKOnly, "Outlook"
End With

'Release memory
Set itmMail = Nothing
Set nsMAPI = Nothing
Set olApp = Nothing
Exit Sub

NewMailMessage_Error:
Error_Handler ("NewMailMessage")
Exit Sub

End Sub
 
Just trying your code

I get an error of "Invalid use of property"

on the line

Set rcpMail.Type = olTo
 
Sorry, my typo. You don't need the keyword Set there. Try just

rcpMail.Type = olTo



--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Just trying your code

I get an error of "Invalid use of property"

on the line

Set rcpMail.Type = olTo
 
Sorry, my typo. You don't need the keyword Set there. Try just

rcpMail.Type = olTo

--
Doug Steele, Microsoft Access MVPhttp://I.Am/DougSteele
(no e-mails, please!)


Just trying your code

I get an error of "Invalid use of property"

on the line

Set rcpMail.Type = olTo
It compiles now, but when run a message box appears saying "A program
is trying to access email address information stored in Outlook."

It has allow,deny and help buttons.

This box never appeared in previous versions of Access when running
the same code.

I presume one can no longer send mail seamlessly via Access to Outlook
 
Sorry, my typo. You don't need the keyword Set there. Try just

rcpMail.Type = olTo

--
Doug Steele, Microsoft Access MVPhttp://I.Am/DougSteele
(no e-mails, please!)


Just trying your code

I get an error of "Invalid use of property"

on the line

Set rcpMail.Type = olTo
I'm starting to think it could be a Vista issue.

A friend has just tested it on XP and it seems fine.

Maybe that allow,deny message box is due to Vista security.

Just a guess......
 
Back
Top