?
Solved

Update Outlook Contact from Access

Posted on 2009-05-06
4
Medium Priority
?
374 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
  • 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
There can be many situations demanding the conversion of Outlook OST files to PST format and as such, there is no shortage of automated tools to perform this conversion. However, what makes Stellar OST to PST converter stand above the rest? Let us e…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

850 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