How to Quickly Create a Contact Group from the Contacts in a Plain Text File via Outlook VBA

If you get a plain text file which contains many contacts, and want to create a new contact group for all these contacts, you can refer to this article. It will teach you to quickly achieve it with effortless ease.

As we all know, Outlook permits users to save a contact group as a plain text file. But it is difficult to create a contact group from a text file. Therefore, here we’ll expose a piece of VBA code, which will automatically read and recognize all the contacts in the TXT file and then add them to a new contact group.

Create Contact Group from Text file

Create a Contact Group from the Contacts in a Plain Text File

  1. At the very beginning, start your Outlook program as normal.
  2. Then you can switch to “Developer” tab and hit “Visual Basic” button. Or just press “Alt + F11” keys.
  3. In the next “Microsoft Visual Basic for Applications” window, you could open a new module.
  4. Subsequently, copy the following VBA code into this module window.
Sub CreateContactGroup_fromTextFile()
    Dim strTextFilePath As String
    Dim objContactGroup As Outlook.DistListItem
    Dim objTempMail As Outlook.MailItem
    Dim objTempRecipient As Outlook.recipient
    Dim objFileSystem As Scripting.FileSystemObject
    Dim objTextStream As Scripting.TextStream
    Dim strLine As String
    Dim strArray() As String
 
    'Change the path to your source Text file
    strTextFilePath = "E:\Colleagues.txt"
    Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    Set objTextStream = objFileSystem.OpenTextFile(strTextFilePath)
 
    'Create a new contact group
    Set objContactGroup = Outlook.Application.CreateItem(olDistributionListItem)
    objContactGroup.Subject = objFileSystem.GetBaseName(strTextFilePath)
    
    On Error Resume Next
    Set objTempMail = Outlook.Application.CreateItem(olMailItem)
 
    'Read each line in the Text File
    Do Until objTextStream.AtEndOfStream
       strLine = objTextStream.ReadLine
       If strLine <> "" Then
          strArray = Split(strLine, vbTab)
          Set objTempRecipient = objTempMail.Recipients.Add(strArray(1))
       End If
    Loop
 
    'Add Group Members
    With objContactGroup
         .AddMembers objTempMail.Recipients
         .Display
    End With
 
    objTextStream.Close
    objTempMail.Close olDiscard
End Sub

VBA Code - Create a Contact Group from the Contacts in a Plain Text File

  1. Eventually, you can try the new macro. Just press F5 key button to trigger this new VBA project.
  2. At once, after the new macro finishes, a new contact group will display.

Notes:

The VBA code above is designed depending on my Plain Text file, which looks like the following screenshot. As you can see, all the contact name and email address is separated via a blank space. If your case is different from mine, you should make some changes in the VBA code.Contacts in Text File

  1. If the contact name and email address is separated via “;”, like the following image, you should change the “strArray = Split(strLine, vbTab)” to “strArray = Split(strLine, “;”)“.Contact name and email address is separated by ";"
  2. If there is no separator, shown as the picture below, you should remove the “strArray = Split(strLine, vbTab)” line and replace “Set objTempRecipient = objTempMail.Recipients.Add(strArray(1))” with “Set objTempRecipient = objTempMail.Recipients.Add(strLine)”.No separator

Resort to a Remarkable PST Fix Utility

Outlook is rich in functions. Even so, it doesn’t mean that Outlook is flawless. It is still quite vulnerable. For instance, it can get corrupt easily due to virus infection. So, it’s prudent to invest in a remarkable PST repair tool like DataNumen Outlook Repair. It can fix various Outlook issues without breaking a sweat and minimize the loss of Outlook data.

Author Introduction:

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

Comments are closed.