Determine if email is actually sent

crazybear

New member
Local time
Today, 09:20
Joined
Sep 19, 2010
Messages
2
Hello,

I am creating a database and am relatively new to VBA.

I have written function to create and send an email with outlook. After the email is created it is displayed using

objOutlookMsg.Display

where objOutlookMsg is a outlook.MailItem declared at the start of the function.

If the user actually sends the message I need to carry out another action within the database. If they don't actually send the message, I don't want to carry out that action.

I can see that there is a .sent property for MailItems but if I test for this it always comes back False - presumably because once the email is gone (either by sending or closing it), it's gone and I can't check that property.

How can I catch whether the email was sent or not?

Many thanks.

Bear.
 
Hi

You could I guess, search the sent items to see if the email is there? If it is then do something, if not do something else.

I'll have a look later as I'm doing email stuff at the minute

Regs

Nidge
 
I think you'd need to set the modal property for display to true. Otherwise the rest of your code will have run before the user has done anything:

Code:
myMailItem.Display(true)

It seems instance of the object is no longer available once it has been sent. So you could test this by querying a property of the instance and catching the error. Seems to work but not very pretty.

I tried using the send event but it only seems to catch the .send method and not the send button in the dialogue!

You'd think the display method would have some return codes from the dialogue. I guess there's a way to obtain this but I've no idea how.

Of course clicking the send button doesn't necessarily mean the mail has been sent. Nigel's suggestion of checking the sent items would be the way to go if that what you really want to test - keep in mind the delay in the time it takes to reach the sent items.

Chris
 
Yes there would be a delay so you could make a separate routine that runs on a timer based on the mail sent. At the end of the email routine, set a variable to false and call the timer. As long as the timer receives false ( which would be set based on the finding of the email), keep checking. As soon as the timer receives true, email is found and stop the timer.

Regs

Nidge
 
I know this is an old post, I have been trying to find a simple solution for the last couple of hours. If its not a critical app the below code fragment should work, test it first to see if it meets your needs, it works for me. The code is in Access 2010 calling Outlook 2010 .

Code:
oMsg.Display True    'make outlook modal
  
  'check if the email was sent or just closed
  On Error Resume Next
  bSent = oMsg.sent            'just used to get a error, 
  If Err = 0 Then
      'no message, email closed, oMsg still exists
      'no message, email saved, oMsg still exists
      bSent = False
  Else
      'message sent, oMsg is null
      bSent = True
  End If
  On Error GoTo mailErr:
cheers,

tom
 

Users who are viewing this thread

Back
Top Bottom