1
votes

I am trying to insert a list of email addresses from Excel into a contact group in Outlook.

I found Excel VBA code online. It uses early binding. It is not an option to force the user to go into Tools-> References -> Outlook, when they open the file.

I need to transform the code from early to late binding.

Questions:

  • I understand that I need to change Outlook.Application to CreateObject('Outlook.Application') and that I can access olFolderContacts with the number 10 instead. See code below.
  • I can't figure out how to access the remaining items such as CreateItem(olDistributionListItem).
Sub CreateContactGroupfromExcel()
Dim objContactsFolder As Outlook.Folder
Dim objContact As Outlook.ContactItem
Dim objContactGroup As Outlook.DistListItem
Dim objNameCell As Excel.Range
Dim objEmailCell As Excel.Range
Dim strName As String
Dim strEmail As String
Dim objTempMail As Outlook.MailItem
Dim objRecipients As Outlook.Recipients

Set objContactsFolder = Outlook.Application.Session.GetDefaultFolder(olFolderContacts)
Set objContactGroup = Outlook.Application.CreateItem(olDistributionListItem)

'You can change the contact group name
objContactGroup.DLName = "PlaceHolder_VBA"

i = 0

Do While Range("vba_email_outlook").Offset(i, 0).Value <> "":
    strName = Range("vba_name_outlook").Offset(i, 0).Value
    strEmail = Range("vba_email_outlook").Offset(i, 0).Value

    Set objContact = objContactsFolder.Items.Find("[FullName] = '" & strName & "'")

    'If there is no such a contact, create it.
    If objContact Is Nothing Then
        Set objContact = Outlook.Application.CreateItem(olContactItem)
        With objContact
            .FullName = strName
            .Email1Address = strEmail
            .Save
        End With
    End If

    'Add the contacts to the new contact group
    Set objTempMail = Outlook.CreateItem(olMailItem)
    objTempMail.Recipients.Add (strName)
    Set objRecipients = objTempMail.Recipients
    objContactGroup.AddMembers objRecipients

    i = i + 1

Loop

'Use "objContactGroup.Save" to straightly save it
objContactGroup.Display
objTempMail.Close olDiscard
End Sub
2
Why the ms-access tag? - June7

2 Answers

0
votes
  1. Declare object variables as generic Object Dim objContactsFolder As Object

  2. Determine number values of constants. With early binding, these values can be seen when hovering over constant or in VBA immediate window: ?olMailItem. Then reference number in place of constant or leave constants referenced as they are and declare them as constants with Const statements. Const olMailItem = 0

olFolderContacts = 10

olMailItem = 0

olDistributionListItem = 7

0
votes

I am not an expert but this code allows you to add the reference when you run the VBA script, but it will mean that if it errors out the code quits running you will not be able to debug.

On Error Resume Next ''' If reference already exist this would cause an error 
Application.VBE.ActiveVBProject.References.AddFromFile ("C:\Program Files (x86)\Microsoft Office\Office16\MSOUTL.OLB") ''' Might have to change file path
On Error GoTo 0