When I send an email which contains the word XYZ in the subject, I want Outlook to copy that email in the folder XY including the sent-date and marked as read.
I found two approaches – both not working:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
If TypeName(Item) <> "MailItem" Then Exit Sub
' ~~> Search for Subject
Set ol = New Outlook.Application
Set olns = ol.GetNamespace("MAPI")
Set myFolder = olns.GetDefaultFolder(olFolderInbox) ' inbox
Set XYFolder = myFolder.Folders("XY")' desired destination folder
If InStr(1, Item.Subject, "XYZ", vbTextCompare) Then
‘ ~~ approach A: copy the object ~~~
Set CopiedItem = Item.Copy ' create a copy
CopiedItem.Move XYFolder ' moce copy to folder
' Set CopiedItem.SendOn = CopiedItem.CreationTime '<- not working, write protected
‘ ~~ approach B: send me a copy (includes using filters afterwards )~~~
Item.CC = Item.CC & "[email protected]"
End If
End Sub
Problems approach A:
The mail items is copied correctly, however the send date and time is blank, as the items has not yet been sent.
Problems approach B:
The new address is added, however as all known addresses are replaced by “user-friendly” names, I get a weird message, that the sender (TO) cannot be resolved any more. Thus the mail will not be sent.
Furthermore I would need to add manual filters – which is rather ugly.
General thoughts
- I want to leave a copy in the send folder. Thus scanning the Send-Folder daily would lead to tons of duplicates in the XY-folder of the same mail.
- Using the Mailitem.SaveMyPersonalItems property would move the mail only in the folder XY but would not leave a copy in sent-folder.
- Possibly the Items.ItemAdd event may be a solution, but I did not yet understand how to check if a new item is added to the sent-folder.
- The built-in filters of outlook allow copying a sent email containing “XYZ” to folder “XY”. However it’s impossible to mark them as read.