More Outlook Resource Sites

Microsoft Developer Network (MSDN)

FAQs and other general resources

share code 23-May-2019 09:48

Looking for help with Outlook programming projects — VSTO, add-ins, VBA, custom Outlook forms, etc.? You′ve come to the right place!

NEW! >> Subscribe to this site via RSS. For more RSS options, see the complete list of feeds on our main news page.

Code level: intermediate    Code area: Basic Outlook Printer Friendly Version
Title: Get information from a linked contact
Description: Outlook is not a relational database. Any folder view shows only the data in that folder, even though Outlook lets you think otherwise by allowing you to add fields from different types of items in the folder, e.g. the Account field from the All Contact Fields list. This VBA code sample shows how to add that Account data from a linked contact into an AppointmentItem's built-in BillingInformation field, which you could then add to the Calendar folder view. It takes advantage of the fact that each resolved Link in the AppointmentItem.Links collection has an Item property that points to the ContactItem.
Date: 02-Sep-2004  05:58
Code level: intermediate
Code area: Basic Outlook
Posted by: Sue Mosher
This message is displayed as VB.NET
 Dim WithEvents m_colCalItems As Outlook.Items

Private Sub Application_Startup()
    Dim objNS As Outlook.NameSpace
    Set objNS = Application.GetNamespace("MAPI")
    Set m_colCalItems = objNS.GetDefaultFolder(olFolderCalendar).Items
    Set objNS = Nothing
End Sub

Private Sub m_colCalItems_ItemAdd(ByVal Item As Object)
    Dim objContact As Outlook.ContactItem
    Dim strAccount As String
    If Item.Links.Count = 1 Then
        Set objContact = Item.Links(1).Item
        strAccount = objContact.Account
        If strAccount <> "" Then
            Item.BillingInformation = strAccount
            Item.Save
        End If
    End If
    Set objContact = Nothing
End Sub


Private Sub m_colCalItems_ItemChange(ByVal Item As Object)
    Dim objContact As Outlook.ContactItem
    Dim strAccount As String
    Select Case Item.Links.Count
        Case 1
            Set objContact = Item.Links(1).Item
            strAccount = objContact.Account
            If strAccount <> Item.BillingInformation Then
                Item.BillingInformation = strAccount
                Item.Save
            End If
        Case 0
            If Item.BillingInformation <> "" Then
            Item.BillingInformation = ""
            Item.Save
        End If
    End Select
    Set objContact = Nothing
End Sub
All 45comments
Page [ 1 2 3 4 5 Next >>  
  02-Sep-2004  13:01   
NOTES:

1) To keep things simple, the sample adds contact information only if the appointment has exactly one linked contact. You could, of course, handle the case of multiple contacts, but I'll leave that as an exercise for the reader to decide how you would want to display that account information.

2) Note that the ItemAdd event handler invokes Item.Save, which will fire the ItemChange event handler, which also invokes Item.Save. The reason this doesn't produce an endless loop is that the ItemChange procedure code checks to see whether the BillingInformation property really needs to be changed and only saves the item if it does. This is an important consideration whenever you're using ItemChange to update items as they are saved.
  02-Sep-2004  13:09   
For other samples using the Links collection, see:

http://www.outlookcode.com/d/code/linkcompany.htm
http://www.outlookcode.com/d/code/clearlinks.htm
http://www.winnetmag.com/MicrosoftExchangeOutlook/Article/ArticleID/22254/MicrosoftExchangeOutlook_22254.html
  18-Oct-2004  16:44   
Hi I am an MCSE that just know a litle about programming. I am interested in the book and in that part of code. I would like to know how to test it. Where I paste that code?

If you can tell me a litle more it will be apreciate
  19-Oct-2004  16:13   
The code above is designed for Outlook VBA. Paste it into the built-in ThisOutlookSession module. If you're new to Outlook VBA macros, these web pages should help you get started:

http://www.winnetmag.com/Articles/Index.cfm?ArticleID=21522&pg=1
http://www.outlookcode.com/d/vb.htm
 
  30-Jul-2006  01:08   
I'm trying to display contact field information in the journal, specifically phone numbers that are associated with a contact. Would this same bit of code apply?

Sue, this site is absolutely terrific!
Angela
  30-Jul-2006  12:50   
Angela, yes, you can apply the same technique to any kind of Outlook tiem.
  06-Feb-2007  19:08   
Hi Sue
I'm running Outlook2003 on Win XP.
I followed up your suggestion to my question in Office NewsGroups, to visit here.
I pasted your code into ThisOutlookSession and made the changes below. (Basically I just changed the references from Calendar to Journal and Account and
BillingInformation fields to BusinessPhone.)
My Macro Security level setting is Low.
When I close Outlook and elect to save VBAProject.OTM, I get a compile error message "Invalid Attribute in Sub or Function"
So the code doesn't run and I can get no further
Am I doing the right thing and what's wrong with the Global Declaration?


Dim WithEvents mcolCalItems As Outlook.Items

Private Sub Application_Startup()
    Dim objNS As Outlook.NameSpace
    Set objNS = Application.GetNamespace("MAPI")
    Set m_colCalItems = objNS.GetDefaultFolder(olFolderJournal).Items
    Set objNS = Nothing
End Sub

Private Sub m_colCalItems_ItemAdd(ByVal Item As Object)
    Dim objContact As Outlook.ContactItem
    Dim strBusinessPhone As String
    If Item.Links.Count = 1 Then
        Set objContact = Item.Links(1).Item
        strBusinessPhone = objContact.BusinessPhone
        If strBusinessPhone <> "" Then
            Item.BusinessPhone = strBusinessPhone
            Item.Save
        End If
    End If
    Set objContact = Nothing
End Sub


Private Sub m_colCalItems_ItemChange(ByVal Item As Object)
    Dim objContact As Outlook.ContactItem
    Dim strBusinessPhone As String
    Select Case Item.Links.Count
        Case 1
            Set objContact = Item.Links(1).Item
            strBusinessPhone = objContact.BusinessPhone
            If strBusinessPhone <> Item.BusinessPhone Then
                Item.BusinessPhone = strBusinessPhone
                Item.Save
            End If
        Case 0
            If Item.BusinessPhone <> "" Then
            Item.BusinessPhone = ""
            Item.Save
        End If
    End Select
    Set objContact = Nothing
End Sub

Your advice would be appreciated
Regards
donwb
  07-Feb-2007  07:29   
donwb, the compiler should highlight the problem statement. What is it?

Also Journal items have no BusinessPhone property. Use the BillingInformation property to store the phone number, as in the original sample, unless you want to add your own custom field to each item with the UserProperties.Add method.
  07-Feb-2007  13:30   
Hi Sue
The problem statement the compiler highlights is all of the first line of code:-
"Dim WithEvents mcolCalItems As Outlook.Items" except for the word "Dim"
I take your point about storing the phone number in BillingInformation.
Hopefully I'll get to that when I've sorted the Dim statement.
Regards
  07-Feb-2007  13:49   
Did you put the code in the built-in ThisOutlookSession module or another class module? Event handers can run only in class modules.
Page [ 1 2 3 4 5 Next >>