A user had this question:
I want to do the following:
- Copy a message from one public folder to another public folder
- Mark the original message as read
- Mark the original Complete
- Add a Category to it
A Quick Step will categorize the e-mail first (it has a higher priority) and both the actual e-mail and the copied one will be 'flagged' with the category. Only the original message should be marked as read and categorized.
While you can't use a quick step, you can use a macro to replicate the steps. The following macro will do everything the quick step did, but you can control the order the steps are completed when you use a macro.
Any fields you want to change on both the original and the copy are done before the Move, any changes that apply to only the original message are done after the copy is moved.
Sub MoveCopyMessage() Dim objNS As Outlook.NameSpace Dim objDestFolder As Outlook.MAPIFolder Dim objItem As Outlook.MailItem Dim objCopy As Outlook.MailItem Set objNS = Application.GetNamespace("MAPI") ' Set the destination folders ' Set objDestFolder = objNS.Folders("Public Folders - alias@domain.com") _ .Folders("All Public Folders").Folders("Old") ' move to a subfolder of the Inbox Set objDestFolder = objNS.GetDefaultFolder(olFolderInbox).Folders("Subfolder") Set objItem = Application.ActiveExplorer.Selection.Item(1) ' copy and move first Set objCopy = objItem.Copy objCopy.Move objDestFolder ' to move ' objItem.Move objDestFolder ' then do whatever With objItem .UnRead = False .MarkAsTask olMarkComplete .Categories = "This Category" .Save End With Set objDestFolder = Nothing Set objNS = Nothing End Sub
Move Opened Message
With a few tweaks, you can move an opened message to a folder. In this example, I'm moving the opened message to a subfolder of the Inbox.
To use this macro, create a macro button on the ribbon or quick access toolbar in an opened message. Then click the button to run the macro.
Sub MoveOpenMessage() Dim objNS As Outlook.NameSpace Dim objDestFolder As Outlook.MAPIFolder Dim objItem As Outlook.MailItem Set objNS = Application.GetNamespace("MAPI") ' Set the destination folder ' move to subfolder of inbox Set objDestFolder = objNS.GetDefaultFolder(olFolderInbox).Folders("Subfolder") ' use selected message ' Set objItem = Application.ActiveExplorer.Selection.Item(1) ' use opened message Set objItem = Application.ActiveInspector.CurrentItem ' then do whatever With objItem .UnRead = False .MarkAsTask olMarkComplete .Categories = "This Category" .Save End With ' to move objItem.Move objDestFolder Set objDestFolder = Nothing Set objNS = Nothing End Sub
Move messages using Folder Picker
If you want to pick the folder to move to you need to use the PickFolder function. However it won't allow you to set a default parent folder. If you want to set a default parent folder, you need to use Redemption.
The first sample below uses Outlook's native PickFolder function.
These macros work on the selected message.
Sub MoveMessagePicker() Dim objNS As Outlook.NameSpace Dim objDestFolder As Outlook.MAPIFolder Dim objItem As Outlook.MailItem Set objNS = Application.GetNamespace("MAPI") ' Pick the destination folders Set objDestFolder = objNS.PickFolder If Not (objDestFolder Is Nothing) Then Set objItem = Application.ActiveExplorer.Selection.Item(1) objItem.Move objDestFolder End If Set objDestFolder = Nothing Set objNS = Nothing End Sub
This version of the macro uses Redemption and allows you to set the parent folder. As shown in the screenshot below, I'm displaying only the folders in the designated path. To show all folders in the data file, change False to True in this line:
dlg.ShowParentFolders = False
Sub MoveMessagePathRDO() ' requires Redemption ' http://dimastr.com/redemption/download.htm Dim Session As Object Dim parentFolder 'As Redemption.RDOFolder Dim rFolder 'As Redemption.RDOFolder Dim objDestFolder As Outlook.MAPIFolder Dim objItem As Outlook.MailItem Set Session = CreateObject("Redemption.RDOSession") Session.MAPIOBJECT = Application.Session.MAPIOBJECT Set dlg = Session.GetSelectFoldersDialog dlg.SelectedFolder = Session.GetDefaultFolder(olFolderInbox).Folders("My Folder") ' to show only the parent and subfolders dlg.ShowParentFolders = False If dlg.Display Then Set rFolder = dlg.SelectedFolder End If If Not (rFolder Is Nothing) Then 'convert Redemption.RDOFolder back to Outlook.Folder Set objDestFolder = Application.Session.GetFolderFromID(rFolder.EntryID) End If If Not (objDestFolder Is Nothing) Then Set objItem = Application.ActiveExplorer.Selection.Item(1) objItem.Move objDestFolder End If Set objDestFolder = Nothing End Sub
How to use the macros on this page
First: You need to have macro security set to the lowest setting, Enable all macros during testing. The macros will not work with the top two options that disable all macros or unsigned macros. You could choose the option Notification for all macros, then accept it each time you restart Outlook, however, because it's somewhat hard to sneak macros into Outlook (unlike in Word and Excel), allowing all macros is safe, especially during the testing phase. You can sign the macro when it is finished and change the macro security to notify.
To check your macro security in Outlook 2010 and newer, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, look at Tools, Macro Security.
After you test the macro and see that it works, you can either leave macro security set to low or sign the macro.
The macros on this page should be placed in a module.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the code in a module:
- Right click on Project1 and choose Insert > Module
- Copy and paste the macro into the new module.
More information as well as screenshots are at How to use the VBA Editor
Hello
I'd like to move mails to severals folders after choosing the folders in a dialog box
Do you have a idea ?
Hi I am trying to use a modified version of the move copy and move to my archive from a specific folder. I am erroring out at the bolded line. The error is Outlook object variable or with block variable not set. I think it has to do with the objItem?
[Code]
Sub MoveDraftMail()
Dim objOutlook As Outlook.Application
Dim objNamespace As Outlook.NameSpace
Dim objSourceFolder As Outlook.MAPIFolder
Dim objDestFolder As Outlook.MAPIFolder
Dim objItem As MailItem
Dim Archive_Folder As Outlook.MAPIFolder
Set objOutlook = New Outlook.Application
Set objNamespace = objOutlook.GetNamespace("MAPI")
Set objSourceFolder = objNamespace.Folders(10).Folders("HouseBillofLadingReport")
Set Archive_Folder = objNamespace.Folders("Online Archive - My@email").Folders("Personal_Folders").Folders("2021").Folders("InBox")
ObjItem.Move Archive_Folder
Set objDestFolder = Nothing
End Sub
[Code\]
Any suggestions.
Thank you
Hi Diane
Would be great if you could help me out. I have a generic mail box accounts.payable@xyz.com where we receive emails from vendors. my team goes into the inbox and checks emails that were sent to accounts.payable@xyz.com and copied to another contact in our organization. If the email is just sent to accounts.payable@xyz.com then team forwards it to the concerned contact and if the email has accounts.payable@xyz.com and the contact of our organization then the team deletes the email. I am looking for a macro that will check if there is accounts.payable@xyz.com in the to or cc and also if there is another contact from our organization, if yes then delete and if the email contains just accounts.payable@xyz.com email without anyone else n our organization then leave it in inbox. I tried rules but it only takes either or Or. Do you think we can achieve this task through a macro? I am not sure if I did a good job explaining my requirement :) hoping I did, really appreciate your help Diane.
Hi Diane,
I need help :)
We have a accounts.payable mail box and we get numerous emails in it. I wanted to know if can create a macro to move emails that contains accounts.payable email in To or CC among others then I want to move them to deleted folder. Our practice is to delete emails that have the staff from departments on an email and accounts payable is copied on it. for example: if I get an email in accounts.payable inbox where, accounts payable is copied in cc or in to: and other staff in my organization is copied on it, I want to delete it. I am not sure if I make sense. :) but any help would be very helpful. It will save my team great deal of manual work to go to each email and delete them or to action them.
thank you Diane
Naveen
Hi, I'm trying to combine two macros to move emails to searched and found folders. In first part I'm searching a folder (for example *jobsite*) and the macro shows the path and -if I choose to- activates the folder. I found this macro on VBoffice_dot_net and it works perfectly.
Now what I want is to move the selected email to this particular folder. I tried to tweak the code above, but somohow, I can't bring the code to move the copied email.
Hi, I am using the MoveCopyMessage module to move to a subfolder called "Subfloder", however I would like to move it to a subfolder of another mailbox. Is this possible? The other mailbox is an account in my outlook. SHANE
As long as the mailbox/folder is in your profile you can move to it. Use one of the methods in this article to get the mailbox - use the getfolderpath method if its an account in the profile and the shared mailbox method if its in a shared mailbox.
https://www.slipstick.com/developer/working-vba-nondefault-outlook-folders/#GetFolderPath
Just noticed that doing a simple MailItem.Move leaves a copy behind in the Recoverable Items folder of the Exchange mailbox. The VBA code is similar to the above, and all it does is move a mail item from the inbox to another (non-default) folder within the same mailbox. The move operation itself works, but I am puzzled that it would also save a copy under Recoverable Items. Moving the same mail item manually in Outlook leaves no such copy behind.
The behavior hints at a hidden copy being made then soft-deleted by MailItem.Move along the way, which is both unnecessary and wasteful. I wonder if/how a "clean" move could be duplicated in VBA code to work like it does in Outlook itself, without the overhead of an extra copy.
My environment is the desktop Outlook 365 running against a (business) office.com account, in case that matters, with the Exchange mailbox being the only account set up in Outlook. Thank you for any insights.
( FWIW I cross-posted the same question on SO here. )
I see Dmitry is answering the SO question - he's the expert on this stuff.
Thanks ¡ ¡¡ It´s a very hepuful and clear post.
Regards.
Javier