Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Docmd.SendObject does not send emails 2

Status
Not open for further replies.

GammelNok

Programmer
Jun 21, 2002
32
0
0
US
This code worked perfectly well in Access 97, but now the event does not fire. Any ideas anyone - help gratefully received

strSendTo = "name@ems.rail.co.xx"
DoCmd.SendObject acSendNoObject, , , strSendTo, _
, , "CD Accepted", "Hi Renae, CD for " & Me!txtStation _
& " Received and Accepted", False

Regards

Hans
 
Hans,

There is no reason why this won't work.....

Craig
 
Wonder if anyone can shed any light on this one:

The following piece of code is repeated for about 7 chaps, and works fine in Access 97, but does not fire in Access 2000.

strSendTo = "xxxx.rail.co.uk"
DoCmd.SendObject acSendNoObject, , , strSendTo, _
, , "CD Accepted", "Hi Hans, CD for " & Me!txtStation _
& " Received and Accepted", False

If I step through in debug mode, the first two chaps get an email, and then the event stops firing.
If I dont go into debug mode, none of the chaps get an email.

It can not be the e-mail addresses, because if I swap chaps around, its still only the first two that fire while stepping through.

I have tried adding DoEvents after the SendObject, but no joy. - I have also tried adding a sleep loop between each Chap, to allow Outlook time to recover. Again no joy.

I have set a Reference to Outlook, and tried moving it up and down. Still no joy.

Any idea as to what might be the cause.
I am totally out of Ideas

Regards

Hans
 
There is aknown bug in Access 2000 regarding the SendObject

The following code solves that

Dim objOutLook As Outlook.Application
Dim objNameSpace As Outlook.NameSpace
Dim objMailItem As Outlook.MailItem

Dim strSendTo As String
Dim strSubject As String
Dim strText As String

'======================================================
Set objOutLook = New Outlook.Application
Set objNameSpace = objOutLook.GetNamespace("MAPI")
Set objMailItem = objOutLook.CreateItem(olMailItem)

strSendTo = "xxxx.yyyy@zzz.co.uk"
strSubject = "Test01"
strText = "Hi xxxx. Your request processed as requested"

With objMailItem
.To = strSendTo
.Subject = strSubject
.Body = strText
.Send
End With

Set objOutLook = Nothing
Set objNameSpace = Nothing
Set objMailItem = Nothing
'======================================================

To send more than one email in a session, the objects will have to be closed and re initialised.
ie the code between '========== and '======== will have to be repeated for each recipient.

 
But only if you've got Outlook installed on your machine....

Craig
 
I have found this code helpful. (The one posted above from )However, I am having a problem trapping errors if the email is not recognized by Outlook.
My Error Handler, which I inserted into the function, always returns a different error number. This is with no changes in the code or table. I just run it, then run again. I have gotten -1421852667, -1318043643, -348110843, and several more.
I want to offer the user the ability to correct the email address, but if I can't trap the error....
Anyone have any idea why this would not be the same. The code is posted below, if anyone thinks it would help.

This is the function being called.
-------------------------------------------------
Public Function fctnOutlook(Optional FromAddr, Optional Addr, Optional CC, Optional BCC, _
Optional Subject, Optional MessageText, Optional Vote As String = vbNullString, _
Optional Urgency As Byte = 1, Optional EditMessage As Boolean = True)


On Error GoTo Error_Handler
' Code sample from Accessory
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient

Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

With objOutlookMsg

If Not IsMissing(FromAddr) Then
.SentOnBehalfOfName = FromAddr
End If

If Not IsMissing(Addr) Then
Set objOutlookRecip = .Recipients.Add(Addr)
objOutlookRecip.Type = olTo
End If

If Not IsMissing(CC) Then
Set objOutlookRecip = .Recipients.Add(CC)
objOutlookRecip.Type = olCC
End If

If Not IsMissing(BCC) Then
Set objOutlookRecip = .Recipients.Add(BCC)
objOutlookRecip.Type = olBCC
End If

If Not IsMissing(Subject) Then
.Subject = Subject
End If

If Not IsMissing(MessageText) Then
.Body = MessageText
End If

If IsNull(Vote) = False Then
.VotingOptions = Vote
End If

Select Case Urgency
Case 2
.Importance = olImportanceHigh
Case 0
.Importance = olImportanceLow
Case Else
.Importance = olImportanceNormal
End Select

For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next

If EditMessage Then
.Display
Else
.Save
.Send
End If

End With
Set objOutlook = Nothing

Exit Function

Error_Handler:
Dim strCorrectedAddress As String
'' Adds the current(and assumedly invalid) email address into a string so that it can be displayed
'' as an error message.
MsgBox "Entering Error Handler"
MsgBox Err.Number
MsgBox Err.Description
Select Case Err.Number
'Invalid Email Address
Case -2147467259
MsgBox "Case One"
Resume
Case -1421852667
MsgBox "Case Two"
Resume
Case -1318043643

MsgBox "Case invalid email."
strErrorMessage = "Outlook does not recognize '" & rstEmailAddresses!Email & "' as a valid email address." & (Chr$(13)) _
& "Please correct the address to a valid format below:"
strCorrectedAddress = InputBox(strErrorMessage)
Call fctnOutlook("sendingaddress", strCorrectedAddress, , , "Testing Send Code", _
strTemplate, , , False)
Resume


'' any other error message
Case Else
MsgBox "None of those were it"
MsgBox Error(Err.Number)
End Select

End Function
 
Hans,
Your code is terrific!!

My docmd.sendobject worked fine in Access97.
But it didn't work in Access2000.

So I tried Microsoft's solution at:

That solution worked ok, but, each time, i got a pop up message from Outlook: "a program is trying to access email addresses you have stored in Outlook. Do you want to allow this? If this is unexpected, it may be a virus and you should choose no."

But your way works great!!
Thanks,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top