Outlook Object Model : How to programmatically get logged-in user’s CompanyName in Outlook 2003/2007?
Recently one of my customer updated that they have a requirement to implement the following logic:
1) Get Contact info from GAL or Address book for the specified contact
2) Get Contact information regarding his CompanyName etc.
In the scenario, i provided the following suggestions:
If you try Outlook 2003 & its prior versions: Using the Microsoft Outlook object model, you can access information that is stored in various address books. For example, you can retrieve information about entries in the Global Address Book, or an Outlook Address Book. But if you want to access additional entries that are typically available for a recipient (such as Office, Title or Phone) you can use the Collaboration Data Objects (CDO) object model.
You can try like this CDO code snippet… This code fragment compares the Address property of the Recipient object with the Address and Type properties of its child AddressEntry object, accessible through the recipients AddressEntry property, to demonstrate the relationships between these properties.
If objOneRecip Is Nothing Then
MsgBox "must select a recipient"
Set objAddrEntry = objOneRecip.AddressEntry
If objAddrEntry Is Nothing Then
MsgBox "no valid AddressEntry for this recipient"
strMsg = "Recipient full address = " & objOneRecip.Address
strMsg = strMsg & "; AddressEntry type = " & objAddrEntry.Type
strMsg = strMsg & "; AddressEntry address = " & objAddrEntry.Address
MsgBox strMsg ' compare display names
strMsg = "Recipient name = " & objOneRecip.Name
strMsg = strMsg & "; AddressEntry name = " & objAddrEntry.Name
For more information about accessing these properties using CDO and detailed information, please see the following articles in the Microsoft Knowledge Base:
HOWTO: Read Address Book Properties in Visual Basic
HOWTO: Work with Distribution Lists Using CDO from VB
Note: CDO 1.2x/MAPI are not supported in a .NET Framework environment. Refer: http://support.microsoft.com/kb/813349
If you work with Outlook 2007 and later, then you can try using ExchangeUser Object. This object provides first-class access to properties applicable to Exchange users. You can also access other properties specific to the Exchange user that are not exposed in the object model through the PropertyAccessor object.
I tried the VBA code sample and obtain the CompanyName.
Dim oExUser As Outlook.ExchangeUser
'Obtain the AddressEntry for CurrentUser
Set oExUser = Application.Session.CurrentUser.AddressEntry.GetExchangeUser
You need to note that, “some of the explicit built-in properties are read-write properties. Setting these properties requires the code to be running under an appropriate Exchange administrator account; without sufficient permissions, calling the ExchangeUser.Update method will result in a "permission denied" error.”
Hope this helps!! Happy programming & happy holidays!!