Solved

Update Outlook Contact from Access

Posted on 2009-05-06
4
366 Views
Last Modified: 2013-11-27
Dear Experts

I have a piece of code for my Access 2003 database that looks up a contact in Outlook 2003 and if they dont exist (the criteria being where "[Profession] = 'OverpaidDirector'") then it adds a new contact.  At present if the contact exists it returns a message box stating XXXXXXXXXXXXXXX  What I would like to do is for the code to take the contact its found and then update various fields (i.e. CompanyName = "Success")

Can anyone supply a piece of code / adapt this one to do that

Many thanks

The code I have is this

Private Sub Command256_Click()
    Dim objApp As Object
    Dim objNamespace As Object
    Dim oF As Object
    Dim objContacts As Object
    Const olFolderContacts = 10
    Const olContactItem = 2
   
    Set objApp = CreateObject("Outlook.Application")
    Set objNamespace = objApp.GetNamespace("MAPI")
    Set oF = objNamespace.GetDefaultFolder(olFolderContacts)
   
    Set objContacts = oF.items.Find("[Profession] = 'OverpaidDirector'")
    If Not TypeName(objContacts) = "Nothing" Then
    MsgBox " XXXXXXXXXXXXXXX "
    Else
        'Create new item if not found
        Set objContacts = objApp.CreateItem(olContactItem)
    End If
        objContacts.Profession = "OverpaidDirector"
        objContacts.FirstName = "BBB"
        objContacts.LastName = "AAAA"
        objContacts.Email1Address = "Email@Addr.com"
        objContacts.Title = "Mr"
        objContacts.CompanyName = "Company"
        objContacts.BusinessTelephoneNumber = "OfficePhone"
        objContacts.MobileTelephoneNumber = "MobilePhone"
        objContacts.BusinessAddressCity = "BusCity"
        objContacts.BusinessAddressCountry = "BusCountry"
        objContacts.BusinessAddressPostalCode = "Zip Code"
        objContacts.BusinessAddressState = "CA"
        objContacts.BusinessAddressStreet = "BusStreet"
        objContacts.BusinessTelephoneNumber = "BusTelNumber"
        objContacts.HomeAddressCity = "HomeCity"
        objContacts.HomeAddressCountry = "HomeCountry"
        objContacts.HomeAddressPostalCode = "Zip Code 2"
        objContacts.HomeAddressState = "AC"
        objContacts.HomeAddressStreet = "HomeStreet"
        objContacts.HomeTelephoneNumber = "HomeTelNumber"
        objContacts.Save
   
    Set objContacts = Nothing
    Set objNamespace = Nothing
    Set objApp = Nothing
End Sub
0
Comment
Question by:correlate
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 24314555
0
 

Author Comment

by:correlate
ID: 24314818
Thanks for that, it looks like its using a very different method to mine & I was keen to build on this piece of code as I'm a bit more familiar with it & didn't really understand the workings of what they were trying to do in the links (i'm not strong technically)
0
 
LVL 65

Accepted Solution

by:
rockiroads earned 500 total points
ID: 24383238
Hi mate, sorry I didnt get round to this earlier.

When u do the find, it returns you the contact object. So objContacts is populated. We have to create objContacts if it doesnt exist

We can then use a flag to identify if its a new or update operation, this is handy where you want certain fields set on a add only


Private Sub Command256_Click()
   
    Dim objApp As Object
    Dim objNamespace As Object
    Dim oF As Object
    Dim objContacts As Object
    Const olFolderContacts = 10
    Const olContactItem = 2
    Dim bNewItem As Boolean
   
    Set objApp = CreateObject("Outlook.Application")
    Set objNamespace = objApp.GetNamespace("MAPI")
    Set oF = objNamespace.GetDefaultFolder(olFolderContacts)
   
    'RETURN THE ContactsObject
    Set objContacts = oF.items.Find("[Profession] = 'OverpaidDirector'")
       
    'Assume record found
    bNewItem = False
   
    'Create new ContactsObject if not found by above
    If TypeName(objContacts) = "Nothing" Then
        Set objContacts = objApp.CreateItem(olContactItem)
        'Flag to indicate new item
        bNewItem = True
    End If
   
    'SET FIELDS
   
    'TO ENSURE YOU SET FIELDS ON ADD ONLY, USE FLAG
    If bNewItem = True Then
        'Following get set on Add only
        objContacts.Profession = "OverpaidDirector"
    End If
   
    'Following get set on add and update
    objContacts.FirstName = "BBB"
    objContacts.LastName = "AAAA"
    objContacts.Email1Address = "Email@Addr.com"
    objContacts.Title = "Mr"
    objContacts.CompanyName = "Company"
    objContacts.BusinessTelephoneNumber = "OfficePhone"
    objContacts.MobileTelephoneNumber = "MobilePhone"
    objContacts.BusinessAddressCity = "BusCity"
    objContacts.BusinessAddressCountry = "BusCountry"
    objContacts.BusinessAddressPostalCode = "Zip Code"
    objContacts.BusinessAddressState = "CA"
    objContacts.BusinessAddressStreet = "BusStreet"
    objContacts.BusinessTelephoneNumber = "BusTelNumber"
    objContacts.HomeAddressCity = "HomeCity"
    objContacts.HomeAddressCountry = "HomeCountry"
    objContacts.HomeAddressPostalCode = "Zip Code 2"
    objContacts.HomeAddressState = "AC"
    objContacts.HomeAddressStreet = "HomeStreet"
    objContacts.HomeTelephoneNumber = "HomeTelNumber"
   
    'SAVE Contact
    objContacts.Save
   
    Set objContacts = Nothing
    Set objNamespace = Nothing
    Set objApp = Nothing
End Sub

0
 

Author Closing Comment

by:correlate
ID: 31578441
Absolute Star - It works perfectly thank you very much
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question