Confirmation of Email Sent

  • Thread starter Thread starter DEI
  • Start date Start date
D

DEI

I have used the .SendObject method in the past to automate Access and
Outlook, i.e. open an email message, populate it, etc. Works fine

However, I am in need of specifiying the 'From' field in the message, so I
am now creating a session, mail item, etc. and defining the
..SentOnBehalfOfName property. Works fine.

However, with the .SendObject method and error trapping, I was able to
determine whether the email was actually sent by the user, and then time
stamp a field in the database. Is there any way of confirming that the email
was sent using the Outlook.Application MailItem? I.e. with the code below?

Dim myOlApp As Outlook.Application
Dim myOlEmail As Outlook.MailItem

Set myOlApp = CreateObject("Outlook.Application")
Set myOlEmail = myOlApp.CreateItem(olMailItem)

Debug.Print myOlEmail.Sent

With myOlEmail
.Subject = "Test"
.To = "to email address"
.Body = "Body"
.SentOnBehalfOfName = "From email address"
.Display
End With

I am open to any ideas/solutions. I can not find away to accomplish both
tasks - populate the 'from' field and confirm that the email was sent, with
one approach.

Thanks,

DEI
 
Try changing your line:-

Dim myOlEmail As Outlook.MailItem

To:-

Dim WithEvents myOlEmail As Outlook.MailItem

You should now be able to handle the Send event of the MailItem and do
whatever you need eg.

Private Sub myOlEmail_Send(Cancel As Boolean)
debug.print "Email to " & myOlEmail.To & " was sent"
End Sub
 
Thank you, Alan.

The solution makes a lot of sense, but I am calling the automation code in a
procedure in a separate module (not attached to a form) and it willnot allow
me to add the WithEvents to the dimesion statement (says it is only valid in
an object model).

When I add the automation code the form itself (button_click), I can use add
WithEvents to the statement, but where/how do I add the private sub
procedure? If I add it to the form, I get an error message.

Thanks.
 
In your VB editor, within the project explorer, right click and insert a new
Class Module (which should be called Class1). Enter the following code (for
example):-
Dim WithEvents myOlEmail As Outlook.MailItem
Dim mySent As Boolean
Public Sub NewMail()
Dim myOlApp As Outlook.Application
Set myOlApp = CreateObject("Outlook.Application")
Set myOlEmail = myOlApp.CreateItem(olMailItem)

With myOlEmail
.Subject = "Test"
.To = "To Email Address"
.Body = "Body"
.SentOnBehalfOfName = "From Email Address"
.Display
End With
End Sub
Private Sub Class_Initialize()
mySent = False
End Sub
Private Sub myOlEmail_Send(Cancel As Boolean)
MsgBox "Sent"
End Sub

Now, whereever you are wanting to run this code from, use the following:-
Dim x As Class1
Set x = New Class1
x.NewMail

Be sure to destroy your objects after use. Hope it helps.
 
Back
Top