| 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 | ||
| Body: |
||
| All 44comments |
| Page [ 1 2 3 4 5 Next >> ] | ||
|
|
Sue Mosher
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. |
|
|
|
Sue Mosher
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 |
|
|
|
David
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 |
|
|
|
Sue Mosher
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 |
|
|
|
Angela
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 |
|
|
|
Sue Mosher
30-Jul-2006 12:50
Angela, yes, you can apply the same technique to any kind of Outlook tiem. |
|
|
|
donwb
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 |
|
|
|
Sue Mosher
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. |
|
|
|
donwb
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 |
|
|
|
Sue Mosher
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 >> ] | ||
| Post your comment name email |
