Partager via


CreateItemFromTemplate()… Importing MSG’s… Where did my UserProperties go?

 

A known behavior of Outlook and the Object Model (documented in this KB) is that if you import an Outlook item into your mailbox or a PST from an MSG file, either by dragging and dropping, or through the CreateItemFromTemplate() (documentation) OOM call, it’s pretty much a known fact that you would find the UserProperties collection (documentation) of the item to be empty, in spite of however many entries may have been contained within it beforehand.

The UserProperties collection can be extremely useful to organizations which have customized their forms to store user-specific information under the hood. Thus, one would definitely not want to find it completely empty, right?

The good news is that when you export an item into an MSG or an OFT file, those UserProperties entries aren’t deleted. Nor are they disposed of when you import that item back into your mailbox. To be able to read those properties back into your items, you can make use of an extremely nifty object called PropertyAccessor, that allows you to read properties that aren’t directly exposed by the Object Model. (The PropertyAccessor object is available only Outlook 2007 onwards, hence consider that one very small part of a ton of excellent reasons to upgrade from prior versions of Office to Office 2010!)

Assuming that you’ve created the Mail Item and assigned it to a variable called oMail, your code would look something like…

 Dim myOlApp As Outlook.Application
Dim MyItem As Outlook.MailItem
Dim propName, propValue As String
Dim oPA As Outlook.PropertyAccessor

Set myOlApp = CreateObject("Outlook.Application")




Set oMail = myOlApp.CreateItemFromTemplate("C:\SampleMailItem.oft",  _
            myOlApp.Session.GetDefaultFolder(olFolderInbox))
'Get a Reference to the PropertyAccessor object
Set oPA = oMail.PropertyAccessor
'Call GetProperty
propName = “CustomProperty1”
propValue = oPA.GetProperty(PropName)
 
 And lo behold, we’re in business!