This macro copies messages, appointments, or contacts stored in the Windows File System back into Outlook.
The macro uses the .OpenSharedItem Method to select the messages and also can be used to import appointments (.msg or .ics) or Contacts (.msg or .vcf) into the Calendar or Contacts folder.
This macro gets the messages in the current folder and copies them into the Outlook folder identified as the Savefolder. It can either be hard-coded or use the current Outlook folder. Messages that cause an error are skipped.
Keep in mind that if you run it on email messages and save to Contacts folder, you'll create contacts, replicating the action of moving a message to contacts.
This macro works in Outlook 2007 and newer.
You need to set a reference to the Microsoft Scripting Runtime in Tools, References. If the reference is not set, you'll receive a Compile error: User-defined type not defined.
Sub ImportMessagesInFolder() Dim fso As Scripting.FileSystemObject Dim SourceFolder As Scripting.Folder Dim SourceFolderName As String Dim FileItem As Scripting.File Dim strFile, strFileType As String Dim oMsg As Object Dim copiedMsg As MailItem Dim Savefolder As Outlook.Folder Set fso = New Scripting.FileSystemObject 'Source folder SourceFolderName = "C:\Users\drcp\Documents\Email\" Set SourceFolder = fso.GetFolder(SourceFolderName) 'Set the Outlook folder name ' Set Savefolder = Session.GetDefaultFolder(olFolderInbox).Folders("My Subfolder") Set Savefolder = Application.ActiveExplorer.CurrentFolder For Each FileItem In SourceFolder.Files Set oMsg = Session.OpenSharedItem(FileItem.Path) On Error Resume Next Set copiedMsg = oMsg.Copy copiedMsg.Move Savefolder Set copiedMsg = Nothing oMsg.Delete Set oMsg = Nothing Next FileItem Set FileItem = Nothing Set SourceFolder = Nothing Set fso = Nothing End Sub
You can use an If statement to limit the macro to email, calendar, or contact items as necessary.
If oMsg.Class = olContact Then Set copiedMsg = oMsg.Copy copiedMsg.Move Savefolder Set copiedMsg = Nothing End if
How to use macros
First: You will need macro security set to low during testing.
To check your macro security in Outlook 2010 or 2013, go to File, Options, Trust Center and open Trust Center Settings, and change the Macro Settings. In Outlook 2007 and older, it’s 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.
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
.OpenSharedItem Method (MSDN)