Check if contact excists in outlook and export to outlook

bodylojohn

New Member
Joined
Dec 23, 2007
Messages
6
Hello,

I have an excel sheet containing a button.

When the button is clicked I want the adress data of my customers exported to outlook.
But if the contact already exists in outlook I want just that contact to be replaced with the data from my excel sheet.
If the contact doesnt exsist I want the contact to be created in outlook.

Thanks in advance...
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Hi

What fields do you have in your Excel sheet (ie what are the headers - I assume you have this sheet of contacts set out in a traditional tabular structure ie Forename, Surname, Email, Address1, Address2 etc ect) and is it the default outlook contacts folder you want these updated to (not a custom contacts folder)? What version of Outlook are you using?
 
Upvote 0
Hi

What fields do you have in your Excel sheet (ie what are the headers - I assume you have this sheet of contacts set out in a traditional tabular structure ie Forename, Surname, Email, Address1, Address2 etc ect) and is it the default outlook contacts folder you want these updated to (not a custom contacts folder)? What version of Outlook are you using?

Hello,

Thank you for responding.
I am using outlook 2003 and the outlook contact default folder.
And I am also using the basic structure: Forename, Surname, Email, Address1
 
Upvote 0
Hi

The following assumes you have your contact list in the following format from A:O (you will need to insert columns/amend data positioning if not):
Sheet1

<table style="font-family: Arial,Arial; font-size: 11pt; background-color: rgb(255, 255, 255); padding-left: 2pt; padding-right: 2pt;" border="1" cellpadding="0" cellspacing="0"> <colgroup><col style="font-weight: bold; width: 30px;"><col style="width: 75px;"><col style="width: 74px;"><col style="width: 57px;"><col style="width: 163px;"><col style="width: 149px;"><col style="width: 158px;"><col style="width: 236px;"><col style="width: 176px;"><col style="width: 110px;"><col style="width: 107px;"><col style="width: 191px;"><col style="width: 146px;"><col style="width: 171px;"><col style="width: 74px;"><col style="width: 45px;"></colgroup><tbody><tr style="background-color: rgb(202, 202, 202); text-align: center; font-weight: bold; font-size: 8pt;"><td> </td><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td><td>F</td><td>G</td><td>H</td><td>I</td><td>J</td><td>K</td><td>L</td><td>M</td><td>N</td><td>O</td></tr><tr style="height: 17px;"><td style="font-size: 8pt; background-color: rgb(202, 202, 202); text-align: center;">1</td><td>FirstName</td><td>LastName</td><td>JobTitle</td><td>BusinessAddressStreet</td><td>BusinessAddressCity</td><td>BusinessAddressState</td><td>BusinessAddressPostalCode (Zip)</td><td>BusinessAddressCountry</td><td>CompanyName</td><td>Email1Address</td><td>BusinessTelephoneNumber</td><td>BusinessFaxNumber</td><td>MobileTelephoneNumber</td><td>NickName</td><td>Notes</td></tr><tr style="height: 17px;"><td style="font-size: 8pt; background-color: rgb(202, 202, 202); text-align: center;">2</td><td>Richard</td><td>Schollar</td><td>Consultant</td><td>5 Mountain View</td><td>Lazytown</td><td>Gloucester</td><td>G56 6tr</td><td>United Kingdom</td><td>Excel Design Solutions</td><td style="color: rgb(0, 0, 255); text-decoration: underline;">richard@somewhere.com</td><td style="text-align: right;">00000000</td><td> </td><td> </td><td>Parsnip</td><td>Chartered Accountant</td></tr></tbody></table>

Then the following code will add/amend contacts:

Code:
Sub UpdateOLContacts()
'set reference to MS Outlook 11 Library
Dim r As Range, varrDetails As Variant, i As Long, strFullName As String
Dim appOutlook As Outlook.Application, nms As Outlook.Namespace
Dim fldContacts As Outlook.Folder, con As Outlook.ContactItem

'have outlook running when you run this code!
'Set outlook objects:
Set appOutlook = GetObject(, "Outlook.Application")
Set nms = appOutlook.GetNamespace("MAPI")
Set fldContacts = nms.GetDefaultFolder(olFolderContacts)

'set worksheet range (header in row 1 - important to use exactly the specified format):
Set r = Range("A2:O" & Cells(Rows.Count, "A").End(xlUp).Row)
'copy range r values
varrDetails = r.Value

'add/amend contacts:
On Error Resume Next
For i = 1 To UBound(varrDetails, 1) Step 1
    strFullName = varrDetails(i, 1) & " " & varrDetails(i, 2)
    'find existing contact:
    Set con = fldContacts.Items(strFullName)
    'check if contact already exists, add a new one if not:
    If con.FullName <> strFullName Then
        Set con = fldContacts.Items.Add
    End If
    'add/amend contact details:
    With con
        .FirstName = varrDetails(i, 1)
        .LastName = varrDetails(i, 2)
        .JobTitle = varrDetails(i, 3)
        .BusinessAddressStreet = varrDetails(i, 4)
        .BusinessAddressCity = varrDetails(i, 5)
        .BusinessAddressState = varrDetails(i, 6)
        .BusinessAddressPostalCode = varrDetails(i, 7)
        .BusinessAddressCountry = varrDetails(i, 8)
        .CompanyName = varrDetails(i, 9)
        .Email1Address = varrDetails(i, 10)
        .BusinessTelephoneNumber = varrDetails(i, 11)
        .BusinessFaxNumber = varrDetails(i, 12)
        .MobileTelephoneNumber = varrDetails(i, 13)
        .NickName = varrDetails(i, 14)
        .Body = varrDetails(i, 15)
        .Close (olSave)
    End With
Next i
On Error GoTo 0
Set fldContacts = Nothing
Set nms = Nothing
Set appOutlook = Nothing
End Sub
Note that you must set a reference to Microsoft Outlook 11 Lib in the VBE (via Tools>References and scroll down the list until you find it, then check it>OK).

I have tested this with Outlook2007 so it is possible that this won't work with Outlook2003 - if not, please post back.
 
Upvote 0
Hello ,

I tested the code in outlook 2007 and it works perfect.
But I need to use outlook 2003 and you guessed it.... it doesnt work.
I get a "type mismatch" in the following code:

Code:
Sub UpdateOLContacts()
'set reference to MS Outlook 11 Library
Dim r As Range, varrDetails As Variant, i As Long, strFullName As String
Dim appOutlook As Outlook.Application, nms As Outlook.Namespace
Dim fldContacts As Outlook.Folders, con As Outlook.ContactItem

'have outlook running when you run this code!
'Set outlook objects:
Set appOutlook = GetObject(, "Outlook.Application")
Set nms = appOutlook.GetNamespace("MAPI")
[B][SIZE="4"]Set fldContacts = nms.GetDefaultFolder(olFolderContacts)[/SIZE][/B]
'set worksheet range (header in row 1 - important to use exactly the specified format):
Set r = Range("A2:O" & Cells(Rows.Count, "A").End(xlUp).Row)
'copy range r values
varrDetails = r.Value

'add/amend contacts:
On Error Resume Next
'For i = 1 To UBound(varrDetails, 1) Step 1
For i = 1 To 1 Step 1
    strFullName = varrDetails(i, 1) & " " & varrDetails(i, 2)
    'find existing contact:
    Set con = fldContacts.Items(strFullName)
    'check if contact already exists, add a new one if not:
    If con.FullName <> strFullName Then
        Set con = fldContacts.Items.Add
    End If
    'add/amend contact details:
    With con
        .FirstName = varrDetails(i, 1)
        .LastName = varrDetails(i, 2)
        .JobTitle = varrDetails(i, 3)
        .BusinessAddressStreet = varrDetails(i, 4)
        .BusinessAddressCity = varrDetails(i, 5)
        .BusinessAddressState = varrDetails(i, 6)
        .BusinessAddressPostalCode = varrDetails(i, 7)
        .BusinessAddressCountry = varrDetails(i, 8)
        .CompanyName = varrDetails(i, 9)
        .Email1Address = varrDetails(i, 10)
        .BusinessTelephoneNumber = varrDetails(i, 11)
        .BusinessFaxNumber = varrDetails(i, 12)
        .MobileTelephoneNumber = varrDetails(i, 13)
        .NickName = varrDetails(i, 14)
        .Body = varrDetails(i, 15)
        .Close (olSave)
    End With
'    If i = 1 Then
'        MsgBox ("i = 1")
'    Else
    
Next i
On Error GoTo 0
Set fldContacts = Nothing
Set nms = Nothing
Set appOutlook = Nothing
End Sub


Can anybody help me with a solution please???
 
Upvote 0
Use this amended code, keep the same sheet structure and the same reference to MS Outlook 11.0:

Code:
Sub UpdateOLContacts()
'set reference to MS Outlook 11 Library
Dim r As Range, varrDetails As Variant, i As Long, strFullName As String
Dim appOutlook As Outlook.Application, nms As Outlook.Namespace
Dim fldContacts As Outlook.MAPIFolder, con As Outlook.ContactItem
'have outlook running when you run this code!
'Set outlook objects:
Set appOutlook = GetObject(, "Outlook.Application")
Set nms = appOutlook.GetNamespace("MAPI")
Set fldContacts = nms.GetDefaultFolder(olFolderContacts)
'set worksheet range (header in row 1 - important to use exactly the specified format):
Set r = Range("A2:O" & Cells(Rows.Count, "A").End(xlUp).Row)
'copy range r values
varrDetails = r.Value
'add/amend contacts:
On Error Resume Next
For i = 1 To UBound(varrDetails, 1) Step 1
    strFullName = varrDetails(i, 1) & " " & varrDetails(i, 2)
    'find existing contact:
    Set con = fldContacts.Items(strFullName)
    'check if contact already exists, add a new one if not:
    If con.FullName <> strFullName Then
        Set con = fldContacts.Items.Add
    End If
    'add/amend contact details:
    With con
        .FirstName = varrDetails(i, 1)
        .LastName = varrDetails(i, 2)
        .JobTitle = varrDetails(i, 3)
        .BusinessAddressStreet = varrDetails(i, 4)
        .BusinessAddressCity = varrDetails(i, 5)
        .BusinessAddressState = varrDetails(i, 6)
        .BusinessAddressPostalCode = varrDetails(i, 7)
        .BusinessAddressCountry = varrDetails(i, 8)
        .CompanyName = varrDetails(i, 9)
        .Email1Address = varrDetails(i, 10)
        .BusinessTelephoneNumber = varrDetails(i, 11)
        .BusinessFaxNumber = varrDetails(i, 12)
        .MobileTelephoneNumber = varrDetails(i, 13)
        .NickName = varrDetails(i, 14)
        .Body = varrDetails(i, 15)
        .Close (olSave)
    End With
Next i
On Error GoTo 0
Set fldContacts = Nothing
Set nms = Nothing
Set appOutlook = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,225,369
Messages
6,184,558
Members
453,243
Latest member
Jemini Jimi

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top