I am trying to create a macro that can automatically forward an email after I send it. I am using Application_ItemSend to trigger a user form with some inputs, then I want to forward the email using some of those inputs. What I'm currently doing works fine if I just send a new email rather than forward the original. I assume the issue is that the original email is not sent until the ItemSend macro finishes and I can't forward an email that hasn't been sent yet. I am wondering how I can write a macro that will run AFTER the email is sent.
CodePudding user response:
You are on the right avenue - you need to wait until the item is sent out. Typically Outlook puts sent items to the Sent Items
folder, so you can hook up the ItemAdd
event on the Sent Items
folder and forward the original sent email.
Public WithEvents myOlItems As Outlook.Items
Public Sub Initialize_handler()
Set myOlItems = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
Dim myOlMItem As Outlook.MailItem
myOlMItem = Item.Forward()
myOlMItem.Recipients.Add "Eugene Astafiev"
myOlMItem.Send
End Sub
Note, users or other add-ins (VBA macros) can set up a custom folder for keeping sent items in Outlook. In that case you need to check out the MailItem.SaveSentMessageFolder property which returns or sets a Folder
object that represents the folder in which a copy of the email message will be saved after being sent. So, you need to set up a hook on that folder too. You can do that in the ItemSend
event handler.