Home > Back-end >  Send and Save specific emails. Not a sentitem event listener
Send and Save specific emails. Not a sentitem event listener

Time:05-14

Issue: Outlook won't receive VBA sent emails until macro completes. If I use wait, sleep, while loops or anything else, the email will not be received. I can't use a Sent Items event listener because I only want specific emails saved to the folder, not all. I individually know how to save from sent items, I know how to send messages. The issue is the delay between sending the email with VBA and it appearing in my Sent Items box. If I saveas before waiting, the email that is saved is the previous email, but if you try to wait, the sent items folder never updates. Any ideas or simple solutions. I might just be dumb.

I have read every forum but haven't found a solution, I had it running at another company but no longer remember how. I use a macro to send very specific emails and do lots of other tasks. I would then like to grab the email I just sent and save it to a folder using vba. The issue is receiving the email in my "Sent Items" folder is delayed. If I try to capture it or wait to receive it, my code never finishes. All solutions I see in the forums are a Sent Item listener / Inbox event listeners, but I don't need that, I know which emails to trigger it on.

CodePudding user response:

One way or another, you'd need to wait for some event to fire, be that Items.ItemAdd on the Sent Items folder, or SyncObject.SyncEnd on the SyncObject - message submission is asynchronous, so no amount of sleep or while loops would help you. You cannot just stop your VBA script and resume it when the message is actually sent since your code is running on the main Outlook thread - if your script pauses, so does Outlook.

CodePudding user response:

You can try using the NameSpace.SendAndReceive method which initiates immediate delivery of all undelivered messages submitted in the current session, and immediate receipt of mail for all accounts in the current profile. Note, calling the SendAndReceive method is asynchronous. SendAndReceive provides the programmatic equivalent to the Send/Receive All command that is available when you click Tools and then Send/Receive.

If you don't need to synchronize all objects, you can use the SyncObjects collection object to select specific objects. For more information, see NameSpace.SyncObjects.

In your VBA code you may also consider using the DoEvents function which yields execution so that the operating system can process other events. DoEvents passes control to the operating system. Control is returned after the operating system has finished processing the events in its queue and all keys in the SendKeys queue have been sent (if any).

DoEvents is most useful for simple things like allowing a user to cancel a process after it has started, for example a search for a file.

  • Related