Refering to Fields in a Contact

C

Chaplain Doug

Outlook 2003. I am experimenting trying to access fields in my contacts. I
have the following code. I can for instance reference the contact field
"FullName" using "itm.FullName" However, how do I access fields like "Home
Phone"? "itm.[Home Phone]" does not work. Thanks for the help.

Dim olApp As New Outlook.Application
Dim olNS As Outlook.Namespace
Dim ctFolder As Outlook.MAPIFolder
Dim ctFolderItems As Outlook.Items
Dim iterateCtItems As Integer
Dim countCtItems As Integer
Dim IC As Integer
Dim Criteria As String
Dim itm As Object

On Error Resume Next
Set olNS = olApp.GetNamespace("MAPI")
Set ctFolder = olNS.Folders("Public Folders")
Set ctFolder = ctFolder.Folders("All Public Folders")
Set ctFolder = ctFolder.Folders("Good News Contacts")
Set ctFolderItems = ctFolder.Items
countCtItems = ctFolderItems.Count
For Each itm In ctFolderItems
DoEvents
debug.print itm.FullName
Next
End Sub
 
M

Michael Bauer

Am Thu, 12 Jan 2006 20:25:02 -0800 schrieb Chaplain Doug:

Please see the Object Browser (F2), switch from <All Libraries> to Outlook
and select MailItem in the left pane. In the right one you can now browse
for all properties etc.
 
C

Chaplain Doug

Thanks Mike. I selected the object browser, Outlook, and ContactItem which
showed a list of the standard field names. But I have added some fields
(user defined fields). How do I access them in my code? (e.g.,
itm.HomeTelephoneNumber is a standard field, I have added a field called
Spouse Birthday but referencing itm.SpouseBirthday gives an error).
--
Dr. Doug Pruiett
Good News Jail & Prison Ministry
www.goodnewsjail.org


Michael Bauer said:
Am Thu, 12 Jan 2006 20:25:02 -0800 schrieb Chaplain Doug:

Please see the Object Browser (F2), switch from <All Libraries> to Outlook
and select MailItem in the left pane. In the right one you can now browse
for all properties etc.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook


Outlook 2003. I am experimenting trying to access fields in my contacts. I
have the following code. I can for instance reference the contact field
"FullName" using "itm.FullName" However, how do I access fields like "Home
Phone"? "itm.[Home Phone]" does not work. Thanks for the help.

Dim olApp As New Outlook.Application
Dim olNS As Outlook.Namespace
Dim ctFolder As Outlook.MAPIFolder
Dim ctFolderItems As Outlook.Items
Dim iterateCtItems As Integer
Dim countCtItems As Integer
Dim IC As Integer
Dim Criteria As String
Dim itm As Object

On Error Resume Next
Set olNS = olApp.GetNamespace("MAPI")
Set ctFolder = olNS.Folders("Public Folders")
Set ctFolder = ctFolder.Folders("All Public Folders")
Set ctFolder = ctFolder.Folders("Good News Contacts")
Set ctFolderItems = ctFolder.Items
countCtItems = ctFolderItems.Count
For Each itm In ctFolderItems
DoEvents
debug.print itm.FullName
Next
End Sub
 
B

Bernie

Doug, the user properties are accessed with the "property" called
"UserProperties". If you are doing after a specific user property then
you have something like...

Sub ShowProperty(dim sName as String )
Dim olApp As Outlook.Application
Dim objContact As Outlook.ContactItem
Dim objContacts As Outlook.MAPIFolder
Dim objNameSpace As Outlook.NameSpace
Dim objProperty As Outlook.UserProperty

Set olApp = CreateObject("Outlook.Application")
Set objNameSpace = olApp.GetNamespace("MAPI")
Set objContacts = objNameSpace.GetDefaultFolder(olFolderContacts)
Set objContact = objContacts.Items.Find("[FileAs] = """ & sName &
"""")
If Not TypeName(objContact) = "Nothing" Then
Set objProperty =
objContact.UserProperties.Find("SpouseBirthday")
If TypeName(objProperty) <> "Nothing" Then
MsgBox "Spouse Birthday Value is: " & objProperty.Value
End If
Else
MsgBox "The spouse birthday was not found."
End If
End Sub

The logic is that the "objContact.UserProperties" returns a collection
of all the "UserProperty". You then can "Find" or "Index" your way
through them to find the one you want. Then you have access to the
"Value" and other properties of that "UserProperty".

As a side note to this. You can scan through all the properties
associated with the contact and pick off the ones that are
"UserProperites" like this...

Dim olcf As Outlook.MAPIFolder
Dim olC As Outlook.ContactItem
Dim olIPs As Outlook.ItemProperties
Dim olIP As Outlook.ItemProperty
Dim olUserP As Outlook.UserProperty
Dim iNumContacts As Long
Dim iNumProps As Integer
..................
Set objItems = olcf.Items
iNumContacts = objItems.Count
If iNumContacts <> 0 Then

For I = 1 To iNumContacts
If TypeName(objItems(I)) = "ContactItem" Then
Set olC = objItems(I)
Set olIPs = olC.ItemProperties
iNumProps = olIPs.Count
If iNumProps <> 0 Then
' Scanning all the properties for this contact.
For j = 0 To iNumProps - 1
Set olIP = olIPs.Item(j)
If olIP.Type <> olOutlookInternal Then
If olIP.IsUserProperty Then
Set olUserP = olIP

'At this point we have a User Property
You can display the olUserP.Formula)
You can display the olUserP.ValidationFormula)
You can display the olUserP.ValidationText)
'
' Here is what you might have an interest in.
'
olUserP.Name - The name of the user property
olUserP.Value - The value of the user property

End If ' Not a user property.
End If ' An internal property so don't look at it.
Next j ' On to next property.
End If 'No properties for this contact - impossible.
End If ' Not a contact entry (maybe distribtion list - skip it)
Next I ' On to next entry in the contacts folder.
End If ' No contacts in folder.

Well hope that answers your question and give you some ideas of what
you can do with them.

Bernie
 
C

Chaplain Doug

THANKS BERNIE and MICHAEL. You gave me my answer. God bless!
--
Dr. Doug Pruiett
Good News Jail & Prison Ministry
www.goodnewsjail.org


Bernie said:
Doug, the user properties are accessed with the "property" called
"UserProperties". If you are doing after a specific user property then
you have something like...

Sub ShowProperty(dim sName as String )
Dim olApp As Outlook.Application
Dim objContact As Outlook.ContactItem
Dim objContacts As Outlook.MAPIFolder
Dim objNameSpace As Outlook.NameSpace
Dim objProperty As Outlook.UserProperty

Set olApp = CreateObject("Outlook.Application")
Set objNameSpace = olApp.GetNamespace("MAPI")
Set objContacts = objNameSpace.GetDefaultFolder(olFolderContacts)
Set objContact = objContacts.Items.Find("[FileAs] = """ & sName &
"""")
If Not TypeName(objContact) = "Nothing" Then
Set objProperty =
objContact.UserProperties.Find("SpouseBirthday")
If TypeName(objProperty) <> "Nothing" Then
MsgBox "Spouse Birthday Value is: " & objProperty.Value
End If
Else
MsgBox "The spouse birthday was not found."
End If
End Sub

The logic is that the "objContact.UserProperties" returns a collection
of all the "UserProperty". You then can "Find" or "Index" your way
through them to find the one you want. Then you have access to the
"Value" and other properties of that "UserProperty".

As a side note to this. You can scan through all the properties
associated with the contact and pick off the ones that are
"UserProperites" like this...

Dim olcf As Outlook.MAPIFolder
Dim olC As Outlook.ContactItem
Dim olIPs As Outlook.ItemProperties
Dim olIP As Outlook.ItemProperty
Dim olUserP As Outlook.UserProperty
Dim iNumContacts As Long
Dim iNumProps As Integer
..................
Set objItems = olcf.Items
iNumContacts = objItems.Count
If iNumContacts <> 0 Then

For I = 1 To iNumContacts
If TypeName(objItems(I)) = "ContactItem" Then
Set olC = objItems(I)
Set olIPs = olC.ItemProperties
iNumProps = olIPs.Count
If iNumProps <> 0 Then
' Scanning all the properties for this contact.
For j = 0 To iNumProps - 1
Set olIP = olIPs.Item(j)
If olIP.Type <> olOutlookInternal Then
If olIP.IsUserProperty Then
Set olUserP = olIP

'At this point we have a User Property
You can display the olUserP.Formula)
You can display the olUserP.ValidationFormula)
You can display the olUserP.ValidationText)
'
' Here is what you might have an interest in.
'
olUserP.Name - The name of the user property
olUserP.Value - The value of the user property

End If ' Not a user property.
End If ' An internal property so don't look at it.
Next j ' On to next property.
End If 'No properties for this contact - impossible.
End If ' Not a contact entry (maybe distribtion list - skip it)
Next I ' On to next entry in the contacts folder.
End If ' No contacts in folder.

Well hope that answers your question and give you some ideas of what
you can do with them.

Bernie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top