Dealing with Outlook Property Sets in CDO


You might have to deal with using Outlook MAPI property identifiers and property sets in your CDO code. For example, because CDO 1.21 does not natively support contacts, you must use Outlook contact properties in CDO using the Fields collection. When you access MAPI properties using CDO 1.21, you must transpose the propset ID. The companion content includes a saved version of the main Web page from that site, which has the list of all the MAPI Outlook properties along with sample code.

When you look at properties in MAPI, you will see that your properties are made up of a property set and a property ID. This is how Outlook and Exchange look up the properties in their own schemas or in your custom schemas. A section of the property range called the public strings section is visible to all applications. Outlook does not create all of its properties in this area. Instead, it creates properties in private property set identifiers. For this reason, if you are going to work with Outlook or Exchange properties that CDO does not have an object for, you must use property set identifiers with the correct property identifier.

An example might make this clearer. You might at some point want to work with Outlook contacts or tasks from CDO. However, CDO does not have a contact or task object. You can use the property sets and property IDs of contacts and tasks to work with these objects. All of the properties used in this example are on the Web for you to download. The following sample shows how to use the SetNamespace method in CDO to set the right property set and also how to read and write properties into Outlook. You can also pass the property set identifier as one of the arguments to the Fields collection, which is also shown in the example.

 Const AMPidTag_BusinessAddress = "0x801B" Const AMPidTag_HomeAddress = "0x801A" Private Sub Command1_Click()      'On Error Resume Next Set oSession = CreateObject("MAPI.Session") oSession.Logon "", "", False, False, 0, True      'Contact Example Set oContactFldr = oSession.GetDefaultFolder(5) Set oContacts = oContactFldr.Messages Set oContact = oContacts.GetFirst Set oFields = oContact.Fields 'Set the namespace to the correct Propset for Contacts oFields.SetNamespace "0420060000000000C000000000000046" strBusinessAddress = oFields(AMPidTag_BusinessAddress).Value strHomeAddress = oFields(AMPidTag_HomeAddress).Value MsgBox "Contact Name: " & oContact.Subject & vbLf _     & "Contact Home Address: " & vbLf _     & strHomeAddress & vbLf & "Contact Business Address: " & vbLf _     & strBusinessAddress      'Task Example Set oTaskFldr = oSession.GetDefaultFolder(8) Set oTasks = oTaskFldr.Messages Set oTask = oTasks.GetFirst Set oFields = oTask.Fields 'Use the fields collection to pass the property set strStartDate = oFields.Item("0x8104", _                "0320060000000000C000000000000046").Value 'Use the other format as well strEndDate = oFields.Item("{0320060000000000C000000000000046}0x8105").Value 'Set the namespace to Tasks oFields.SetNamespace "0320060000000000C000000000000046" boolCompleted = CBool(oFields.Item("0x810F").Value) MsgBox "Task Name: " & oTask.Subject & vbLf _     & "Task Start Date: " & vbLf _     & strStartDate & vbLf & "Task End Date: " & vbLf _     & strEndDate & vbLf & "Task Completed: " & boolCompleted End Sub 

Remember that Outlook does more than just set properties on items. For example, when you change the home city for a contact in Outlook, Outlook has code that takes the change and puts it into the home address property (which is a combination of street, city, state, and Zip code). If you write code in CDO that uses Outlook properties but does not populate all the properties that Outlook expects, you can cause Outlook to malfunction. For this reason, when you're using the properties described earlier, you should test your code thoroughly with Outlook. Another way to figure out what properties Outlook expects is to perform the actions in Outlook and look at what happens in the MAPI properties using MDBVUE.




Programming Microsoft Outlook and Microsoft Exchange 2003
Programming MicrosoftВ® OutlookВ® and Microsoft Exchange 2003, Third Edition (Pro-Developer)
ISBN: 0735614644
EAN: 2147483647
Year: 2003
Pages: 227
Authors: Thomas Rizzo

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net