If you want to merge the contents of multiple text files and then import the contents into an Outlook email, you can utilize the approach shared in this article. It’ll allow you to achieve that in quick time.
Instead of insert multiple text files as attachments into an email, some users hope to directly import the contents of many text files into an email’s message body. It is apparent that you cannot realize it with Outlook native functions. Hence, you need to utilize other means, such as VBA code. Now, in the followings, we will try importing all the text files in a Windows folder into an email by using a macro.
Merge & Import the Contents of Multiple Text Files into an Email
- At the very outset, start your Outlook application.
- Then, you need get access to Outlook VBA editor by following the steps listed in the article – “How to Run VBA Code in Your Outlook”.
- Next, you need enable “Microsoft Word Object Library” with reference to the article – “How to Add an Object Library Reference in VBA”.
- Next, copy and paste the VBA code below into an unused module.
Sub ImportMultipleTextFilesIntoEmail() Dim objShell As Object Dim objWindowsFolder As Object Dim strWindowsFolder As String Dim objFileSystem As Object Dim strFile As String Dim objWordApp As Word.Application Dim objTempDocument As Word.Document Dim objTextFile As Word.Document Dim objMail As Outlook.MailItem Dim objMailDocument As Word.Document 'Select a Windows folder Set objShell = CreateObject("Shell.Application") Set objWindowsFolder = objShell.BrowseForFolder(0, "Select a Windows Folder:", 0, "") If Not objWindowsFolder Is Nothing Then strWindowsFolder = objWindowsFolder.self.Path & "\" Set objFileSystem = CreateObject("Scripting.FileSystemObject") Set objWindowsFolder = objFileSystem.GetFolder(strWindowsFolder) Set objWordApp = CreateObject("Word.Application") Set objTempDocument = objWordApp.Documents.Add objWordApp.Visible = True 'Import the contents of all text files into a Word document strFile = Dir(strWindowsFolder & "\*.txt", vbNormal) While strFile <> "" Set objTextFile = objWordApp.Documents.Open(strWindowsFolder & "\" & strFile) objTempDocument.Range.InsertAfter objTextFile.Range.Text & vbCr objTextFile.Close SaveChanges:=True strFile = Dir() Wend 'Copy the content of Word document into an email's message body objTempDocument.Content.Copy Set objMail = Application.CreateItem(olMailItem) Set objMailDocument = objMail.GetInspector.WordEditor objMailDocument.Content.Paste objMail.Display objTempDocument.Close False objWordApp.Quit End If End Sub
- After that, you can run the macro by pressing “F5” key button.
- Later, you will be required to select a Windows folder.
- Immediately, an email will display, which is containing all the contents of all text files in the selected Windows folder.
Keep a Formidable Repair Tool Nearby
Outlook comes packed with quantities of excellent features. However, it does not mean that it is flawless. One of its fatal drawbacks is its vulnerability. Therefore, it is necessary for regular users to keep a robust and trusty Outlook fix tool, such as DataNumen Outlook Repair. This utility is able to scan the Outlook file and check it for errors and resolve them in quick time.
Shirley Zhang is a data recovery expert in DataNumen, Inc., which is the world leader in data recovery technologies, including recover mdf and outlook repair software products. For more information visit www.datanumen.com