For convenience, you may wish to store some frequently-used files, such as Word documents, Excel workbooks or JPEG images, in an Outlook folder. This article will share a piece of VBA code, which can help you batch import such files into Outlook.
My previous article- “4 Effective Tips for Storing Files in Your Outlook Mailbox” has exposed that Outlook can be used to store files, no matter Word documents, Excel workbooks, PowerPoint presentations, Zip archives or pictures, etc. Thus, it is advisable to store your frequently-needed files in an Outlook folder. Here we’ll show you how to batch import the files from a Windows folder to an Outlook folder.
Batch Import Files from a Windows Folder to an Outlook Folder
- To begin with, start your Outlook program.
- Then in the main Outlook screen, you could press “Alt + F11” keys.
- Next in the new “Microsoft Visual Basic for Applications” window, you should open an unused module.
- Subsequently, you ought to copy the following VBA code into this module.
Sub ImportFiles_WindowsFolder_OutlookFolder() Dim objShell As Object Dim objWindowsFolder As Object Dim strWindowsFolder As String Dim objOutlookFolder As Outlook.Folder 'Select a source Windows folder Set objShell = CreateObject("Shell.Application") Set objWindowsFolder = objShell.BrowseForFolder(0, "Select the source folder:", 0, "") If Not objWindowsFolder Is Nothing Then strWindowsFolder = objWindowsFolder.self.Path & "\" 'Select a destination Outlook folder Set objOutlookFolder = Outlook.Application.Session.PickFolder 'Start import Call ProcessFolders(strWindowsFolder, objOutlookFolder) MsgBox "Import Successfully!", vbExclamation End If End Sub Sub ProcessFolders(ByRef strFolderPath As String, ByVal objOutlookFolder As Outlook.Folder) Dim objSubFolder As Object Dim objFileSystem As Object Dim objFile As Object Dim strFileExtension As String Dim strFilePath As String Dim objDocumentItem As Outlook.DocumentItem Dim objAttachment As Outlook.Attachment Set objFileSystem = CreateObject("Scripting.FileSystemObject") Set objWindowsFolder = objFileSystem.GetFolder(strFolderPath) 'Process all files in the source Windows folder For Each objFile In objWindowsFolder.Files strFilePath = objFile.Path Set objDocumentItem = objOutlookFolder.Items.Add("IPM.Document") Set objAttachment = objDocumentItem.Attachments.Add(strFilePath) objDocumentItem.Subject = objAttachment.filename 'Add corresponding document items based on the source file type strFileExtension = objFileSystem.GetExtensionName(objFile) Select Case strFileExtension Case "doc", "docx" objDocumentItem.MessageClass = "IPM.Document.Word.Document" Case "xls", "xlsx" objDocumentItem.MessageClass = "IPM.Document.Excel.Sheet" Case "jpg" objDocumentItem.MessageClass = "IPM.Document.jpegfile" Case "pdf" objDocumentItem.MessageClass = "IPM.Document.AcroExch.Document" Case "pps", "pptx" objDocumentItem.MessageClass = "IPM.Document.PowerPoint.Show" Case "pub", "pubx" objDocumentItem.MessageClass = "IPM.Document.Publisher.Document" Case Else objDocumentItem.MessageClass = "IPM.Document." & strFileExtension & "file" End Select objDocumentItem.Save Next 'Process all subfolders recursively For Each objSubFolder In objWindowsFolder.SubFolders 'Skip hidden and system folders If ((objSubFolder.Attributes And 2) = 0) And ((objSubFolder.Attributes And 4) = 0) Then Call ProcessFolders(objSubFolder.Path, objOutlookFolder) End If Next objSubFolder End Sub
- After that, you should insure that macros are allowed in your Outlook.
- Eventually, you could try this macro.
- First, hit the “Run” icon in the toolbar of VBA editor or just press F5 key.
- And then you will be required to select a source Windows folder.
- Next you need to choose a destination Outlook folder.
- After you receive the message “Import Successfully”, you will see that all the files in source local folder have been imported to the chosen Outlook folder, like the screenshot below:
Make a Regular Outlook Data Backup
As a part of Microsoft Office suite, Outlook indeed has myriad features related to other Office programs. These capacities do improve our work efficiencies a lot. Nevertheless, Outlook is not flawless. It is vulnerable to corruption. Thus, so as to prevent Outlook corruption, you ought to make a regular backup, such that even if PST file becomes corrupted, you still can get back your PST data from backups.
Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including repair mdf and outlook repair software products. For more information visit www.datanumen.com