Use a macro to move Sent Items
I hear from a lot of users who don't like how Outlook handles sent items with an IMAP account. In older versions of Outlook, IMAP sent items use the local Sent Items folder. While Outlook 2007 and 2010 can be configured to use the IMAP sent folder, when you use a Send by email command in other applications, the sent item goes into the local data file, ignoring the account configuration. Outlook 2013 uses XLIST from the IMAP server to determine the sent items folder, but if the IMAP server doesn't support XLIST, the sent item might be saved locally. Outlook 2016 and newer handles it better, using the folder the server uses or one named Sent Items.
For Exchange users, Sent Items are stored in the account's mailbox (and have an option use the option to Save sent items with the original, if not in the Inbox). But Shared mailboxes save a copy in the users own Sent Items folder by default, but the shared mailbox may be configured save a copy in the shared mailbox sent folder too.
You can solve this problem by monitoring the Sent Items folder and moving sent messages to the desired folder.
Outlook 2013 users who want to move IMAP messages to a local pst file can use this macro too, however, it will not move the messages immediately. They need to sync down from the IMAP server first.
To use the macros on this page, you need to use the GetFolderPath macro from [URL='www.slipstick.com/developer/working-vba-nondefault-outlook-folders/']GetFolderPath.
You'll also need to [URL='www.slipstick.com/developer/how-to-use-outlooks-vba-editor/']set macro security to low to test the macro.
Press Alt+F11 to open the VBA editor. Expand Project1 and Microsoft Outlook Objects then double click on ThisOutlookSession. Paste the code into ThisOutlookSession. GetFolderPath can be pasted below this macro or in a new module.
To use this with multiple accounts, copy the IF… End IF block and change the account name and folder path for each account.
The account name and the data file name are usually the same – in Outlook 2010 and newer, both are the email address by default.
You need to use the account name as seen in File, Account Settings (or in the From field if you have multiple accounts) and the data file name (and path to the Sent folder) as seen in the folder list.
cdn.slipstick.com/images/account-file-names.png
You can get the path to the sent folder from folder properties. Right click on the sent folder, choose Properties. Select the path from the Location field (without the \) and the folder name from the name field.
cdn.slipstick.com/images/2012/code-samples/sent-folder-path.png
Note: this macro will work with any account type.
These macros start when Outlook starts. To test it without restarting Outlook, click in the Application_Startup macro and click the Run button (or press F5).
Only move Sent mail from one account
This macro checks the sender address and only moves mail sent from that address.
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Set Items = Session.GetDefaultFolder(olFolderSentMail).Items
End Sub
Private Sub Items_ItemAdd(ByVal Item As Object)
If Item.SendUsingAccount = "alias@domain.com" Then
' Get the GetFolderPath function from https://slipstick.me/getfolderpath
Set MovePst = GetFolderPath("data file nameInboxSent")
Item.Move MovePst
End If
End Sub
Move Sent mail from Shared Mailbox
This version of the macro works with Exchange Server shared mailboxes. It can either use the GetfolderPath function or resolve the shared mailbox. The Exchange server needs to be configured to save sent items in the shared mailbox.
Private WithEvents olSentItems As Items
Dim olSent As folder
Dim olInbox As folder
Private Sub Application_Startup()
Dim objNS As NameSpace
Set objNS = Application.Session
' Uses GetFolderPath Function
' Set olSentItems = GetFolderPath("Outlook SalesSent Items").Items
' Set olInbox = GetFolderPath("Outlook SalesInbox")
' looks up shared mailbox name
Dim objOwner As Outlook.Recipient
Set NS = Application.GetNamespace("MAPI")
Set objOwner = NS.CreateRecipient("olsales")
objOwner.Resolve
If objOwner.Resolved Then
' MsgBox objOwner.Name
Set olInbox = objNS.GetSharedDefaultFolder(objOwner, olFolderInbox)
' Because using objNS.GetSharedDefaultFolder(objOwner, olFolderSentMail) triggers an error
Set olSentFolder = olInbox.Parent.Folders("Sent Items")
Set olSentItems = olSentFolder.Items
End If
Set objNS = Nothing
End Sub
Private Sub olSentItems_ItemAdd(ByVal Item As Object)
MsgBox "New Item"
Item.Move olInbox
End Sub
Using Multiple IMAP accounts in Outlook 2013 and newer
Beginning with Outlook 2013, IMAP sent messages are stored in the IMAP account's sent folder and you'll need to watch each sent folder for new items.
Because each account in this example is moving sent items to the same data file, we can set the MovePst as a global variable.
This macro will also work with Exchange accounts and POP accounts that have their own pst files for incoming mail.
Private WithEvents Items As Outlook.Items
Private WithEvents GmailItems As Outlook.Items
Private WithEvents AliasItems As Outlook.Items
Dim MovePst As Outlook.Folder
Private Sub Application_Startup()
'watch default account sent folder
Set Items = Session.GetDefaultFolder(olFolderSentMail).Items
' Use the GetFolderPath function from https://slipstick.me/getfolderpath
'watch other sent folders
Set GmailItems = GetFolderPath("me@gmail.com[Gmail]Sent Mail").Items
Set AliasItems = GetFolderPath("diane@domain.comSent Items").Items
' set move pst
Set MovePst = GetFolderPath("Outlook Data FileSent Items")
End Sub
Private Sub Items_ItemAdd(ByVal Item As Object)
Item.Move MovePst
End Sub
Private Sub GmailItems_ItemAdd(ByVal Item As Object)
Item.Move MovePst
End Sub
Private Sub AliasItems_ItemAdd(ByVal Item As Object)
Item.Move MovePst
End Sub
Multiple IMAP accounts in Outlook 2010 and older
If you are using multiple accounts, you can use IF statements to selectively move messages. This works with Outlook 2010 and older only, because Sent items are moved into the default pst.
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Set Items = Session.GetDefaultFolder(olFolderSentMail).Items
End Sub
Private Sub Items_ItemAdd(ByVal Item As Object)
' Get the GetFolderPath function from https://slipstick.me/getfolderpath
If Item.SendUsingAccount = "alias@domain.com" Then
Set MovePst = GetFolderPath("data file nameInboxSent")
ElseIf Item.SendUsingAccount = "alias@domain1.com" Then
Set MovePst = GetFolderPath("data file name1InboxSent")
ElseIf Item.SendUsingAccount = "alias@domain2.com" Then
Set MovePst = GetFolderPath("data file name2InboxSent")
Else
Exit Sub
' Item.UnRead = False
Item.Move MovePst
End If
End Sub
You could also use Case statements to set the correct Move path.
[URL='www.slipstick.com/developer/code-samples/use-a-macro-to-move-sent-items/#all']
Move all sent messages
This macro assumes you have only one IMAP account in Outlook (and it's set as default data file) or one or more POP accounts in Outlook using the same data file and want to move the mail sent from all accounts to a different folder.
Outlook 2013 (and newer) users who want to move IMAP messages to a local pst file can use this macro too, however, it may not move the messages immediately. Sent messages need to sync down from the IMAP server first.
Private WithEvents Items As Outlook.Items
Private Sub Application_Startup()
Set Items = Session.GetDefaultFolder(olFolderSentMail).Items
End Sub
Private Sub Items_ItemAdd(ByVal Item As Object)
' You need the GetFolderPath function from https://slipstick.me/getfolderpath
Set MovePst = GetFolderPath("data file nameInboxSent")
Item.UnRead = False
Item.Move MovePst
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 [URL='www.slipstick.com/developer/how-to-use-outlooks-vba-editor/#selfcert']sign the macro.
Macros that run when Outlook starts or automatically need to be in ThisOutlookSession, all other macros should be put in a module, but most will also work if placed in ThisOutlookSession. (It's generally recommended to keep only the automatic macros in ThisOutlookSession and use modules for all other macros.) The instructions are below.
The macros on this page need to go into ThisOutlookSession.
Open the VBA Editor by pressing Alt+F11 on your keyboard.
To put the macro code in ThisOutlookSession:
- Expand Project1 and double click on ThisOutlookSession.
- Copy then paste the macro into ThisOutlookSession. (Click within the code, Select All using Ctrl+A, Ctrl+C to copy, Ctrl+V to paste.)
More information as well as screenshots are at [URL='https://www.slipstick.com/developer/how-to-use-outlooks-vba-editor/']How to use the VBA Editor
More Information
[URL='www.slipstick.com/outlook/outlook-2010/messages-arent-saved-in-the-sent-items-folder/']Messages Aren't Saved in the Sent Items Folder
[URL='www.slipstick.com/outlook/config/configure-and-use-imap-accounts/']Configure and Use IMAP Accounts
[URL='www.slipstick.com/outlook/email/choosing-the-folder-to-save-a-sent-message-in/']Choosing the Folder to Save a Sent Message In
www.slipstick.com/developer/code-samples/use-a-macro-to-move-sent-items/
Xây dựng Lương 3P, KPI cho Doanh nghiệp
Làm thế nào để trả lương cho nhân viên chính xác nhất? Đây là một trong những câu hỏi khó trong quản trị nhân...
Xem khóa học
Bình luận