Hey guys, I'm using a macro I found on the internet to import contacts from excel to outlook. I tailored it to suit my sheet and needs and it's working well. The only thing is, if a contact is already in outlook it still creates a new contact with the same details (therefore making it a duplicate). Is there a code to ensure that excel does not import duplicate contacts using VBA?
Here is the code
Code
Sub CommandButton4_Click()
Dim applOutlook As Outlook.Application
Dim nsOutlook As Outlook.Namespace
Dim ciOutlook As Outlook.ContactItem
Dim delItems As Outlook.Items
Dim lLastRow As Long, i As Long, n As Long, c As Long
'determine last data row in the worksheet:
lLastRow = Sheets("Sheet1").Cells(Rows.Count, "E").End(xlUp).Row
'Create a new instance of the Outlook application. Set the Application object as follows:
Set applOutlook = New Outlook.Application
'use the GetNameSpace method to instantiate (ie. create an instance) a NameSpace object variable, to access existing Outlook items. Set the NameSpace object as follows:
Set nsOutlook = applOutlook.GetNamespace("MAPI")
'----------------------------
'----------------------------
'post each row's data on a separate contact item form:
For i = 2 To lLastRow
'Use the Application.CreateItem Method to create a new contact Outlook item in the default Contacts folder. Using this method a new contact item is always created in the default Contacts folder.
'create and display a new contact form for input:
Set ciOutlook = applOutlook.CreateItem(olContactItem)
'display the new contact item form:
ciOutlook.Display
'set properties of the new contact item:
With ciOutlook
.FirstName = ActiveSheet.Cells(i, 5)
.LastName = ActiveSheet.Cells(i, 6)
.Email1Address = ActiveSheet.Cells(i, 8)
.MobileTelephoneNumber = ActiveSheet.Cells(i, 7)
End With
'close the new contact item form after saving:
ciOutlook.Close olSave
Next i
'clear the variables:
Set applOutlook = Nothing
Set nsOutlook = Nothing
Set ciOutlook = Nothing
Set delFolder = Nothing
Set delItems = Nothing
End Sub
Display More
Kind Regards,
Julian