Home > Net >  Outlook - Move any email deleted from certain folders to a subfolder of Deleted Items instead
Outlook - Move any email deleted from certain folders to a subfolder of Deleted Items instead

Time:10-13

I have a client that would like any email deleted from within certain folders in Outlook to go into a sub-folder of her Deleted Items automatically. There are 3 different folders she would like this to happen for, all are sub-folders of her Inbox.

I am mildly VBA savvy, meaning I can usually hack something together from similar code to make it fit what I am trying to accomplish, but I have not been able to find anything close enough to what she is asking for that I could modify to fit this need.

Any tips would be appreciated.

Edit:

Adding the code I used in case someone else could benefit from it:

Dim WithEvents olFolder1 As Outlook.Folder
Dim WithEvents olFolder2 As Outlook.Folder
Dim WithEvents olFolder3 As Outlook.Folder
Dim olDelFolder As Outlook.Folder
Dim olDestFolder As Outlook.Folder

Private Sub Application_Startup()
Set olFolder1 = Application.Session.GetDefaultFolder(olFolderInbox)
Set olFolder1 = olFolder1.Folders("Subfolder of Inbox Name")
Set olFolder1 = olFolder1.Folders("Subfolder of Subfolder Name")
Set olFolder2 = Application.Session.GetDefaultFolder(olFolderInbox)
Set olFolder2 = olFolder2.Folders("Subfolder of Inbox Name")
Set olFolder2 = olFolder2.Folders("Subfolder of Subfolder Name")
Set olFolder3 = Application.Session.GetDefaultFolder(olFolderInbox)
Set olFolder3 = olFolder3.Folders("Subfolder of Inbox Name")
Set olFolder3 = olFolder3.Folders("Subfolder of Subfolder Name")
Set olDelFolder =  Application.Session.GetDefaultFolder(olFolderDeletedItems)
Set olDestFolder = olDelFolder.Folders("Subfolder of Deleted Items Name")
End Sub

Private Sub olFolder1_BeforeItemMove(ByVal Item As Object, ByVal MoveTo As MAPIFolder, Cancel As Boolean)
If MoveTo = olDelFolder Then
Cancel = True
Item.Move olDestFolder
Else
Cancel = False
End If
End Sub

Private Sub olFolder2_BeforeItemMove(ByVal Item As Object, ByVal MoveTo As MAPIFolder, Cancel As Boolean)
If MoveTo = olDelFolder Then
Cancel = True
Item.Move olDestFolder
Else
Cancel = False
End If
End Sub

Private Sub olFolder3_BeforeItemMove(ByVal Item As Object, ByVal MoveTo As MAPIFolder, Cancel As Boolean)
If MoveTo = olDelFolder Then
Cancel = True
Item.Move olDestFolder
Else
Cancel = False
End If
End Sub

Note:

If a message is manually moved to the Deleted Items folder instead of being deleted in a traditional way, a copy of the message ends up in both the Deleted Items and the specified Sub-folder. Not sure why that is, but the end user won't be manually moving messages like that so it wasn't something I needed to fix.

CodePudding user response:

Use the Folder.BeforeItemMove event which is fired when an item is about to be moved or deleted from a folder, either as a result of user action or through program code. The second parameter represents the folder to which the item is being moved. So, you can check whether it is Deleted Folder or not. If so, you may cancel the default action by setting the third parameter to true. Set the Cancel parameter to `true to cancel the move or delete action. Then you may programmatically move the item to the required destination instead.

CodePudding user response:

I'd rather use Items.ItemAdd event on the Deleted Items folder. Unlike the Folder.BeforeItemMove event (which is UI specific and does not fire in all cases), Items.ItemAdd event is much more reliable.

  • Related