Error 287 - Access 2007 with Outlook

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?
 
D

Douglas J. Steele

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?
 
T

troy23

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
 
D

Douglas J. Steele

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
 
T

troy23

Just trying your code

I get an error of "Invalid use of property"

on the line

Set rcpMail.Type = olTo
 
D

Douglas J. Steele

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
 
T

troy23

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
 
T

troy23

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......
 

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