SaveAs() sometimes loses userProperty!

P

PuppetMaster

I am creating a VB COM addin for Outlook 2002 and have come up against
the following problem. Sometimes when I create a custom UserProperty
for a mailItem, when I then do a SaveAs() on this item, the
UserProperty disappears.

It seems that the first time I run the code below, it works fine, but
if I run it a second time, it fails! Also, if make a selection of
emails and loop round this code for each one of them, some of the mails
will have the UserProperty in the SaveAs() item, and some won't?!

Can anyone else replicate this problem, or know why this may be
happening?

--------------------------------------
Dim userprop As UserProperty
Set userprop = m4addin.itemObj.UserProperties.Add("archiveTo", olText,
True)
userprop.Value = "C:\temp"
m4addin.itemObj.Save
MsgBox m4addin.itemObj.UserProperties("archiveTo").Value '=OK
Call m4addin.itemObj.SaveAs(myConfig.HDDq & projectFilename & ".msg")
'At this point the customer property may have disappeared!
Dim foo As MailItem
Set foo = m4addin.oApp.CreateItemFromTemplate(myConfig.HDDq &
projectFilename & ".msg")
MsgBox foo.UserProperties.Count
Set foo = Nothing
 
D

Dave Kane [MVP - Outlook]

CreateItemFromTemplate takes an OFT file as its argument. Try

Call m4addin.itemObj.SaveAs(myConfig.HDDq & projectFilename &
".oft",olTemplate)
and
Set foo = m4addin.oApp.CreateItemFromTemplate(myConfig.HDDq &
projectFilename & ".oft")
 
S

Sue Mosher [MVP-Outlook]

Actually, it can also take an .msg file, as well as an .oft file, but it would be worth seeing if an .oft file works better in this scenario.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
P

PuppetMaster

Thank you for the suggestions, however a definite specification for
this application is that the messages are saved in .msg format. To
satisfy my curiosity I tried saving as an .oft anyway, but it did not
seem to make any difference.

My reasons for wanting to delete the userProperty were mainly due to
the Items.Find function, where I want to filter the items returned
based on whether this field exists or not. From my experience, I have
found that: -

1. When adding a custom field, a kind of "index" is created by Outlook
which enables the ability to use items.find() on that field. However,
this index is only per folder, so if this message item is moved to
another folder, the find function will not work. To resolve this,
either the user needs to manually add the field to their custom view,
or a dummy custom-field add must be repeated for each folder where the
search filter is required to force creation of this index.

2. This "index" mentioned above is automatically created and updated on
item.customProperty.add. No item.save is necessary since the actually
object structure is being changed.

3. When deleting a custom property, the "index" is not actually updated
until an item.save is performed as well! This can be seen by putting
the customProperty field into the folder view and watching the value
change (or not). However, as given in the code in my OP, when
performing the following commands below for the second time on the same
mail item this somehow corrupts the "saveAs" copy and the
customProperty is no longer there! This happens even if I drop the item
after saveAs() and re-reference it from Outlook by its item.EntryID.
item.saveAs()
item.customProperty.delete
item.save

4. To get around this problem, I have simply removed the
item.customProperty.delete and just altered the value of it to "". Then
changed the find filter to
collection.Find("[archiveTo] <> '' and [archiveTo] <> nothing ")

Does this whole problem look like an Outlook bug?

-------------------------
'Work around code
Dim userprop As UserProperty
Set userprop = m4addin.itemObj.userproperty.add "archiveTo", olText,
True
userprop.value = archiveLocation
Call m4addin.itemObj.SaveAs(myConfig.HDDq & projectFilename & ".msg")
m4addin.itemObj.UserProperties("archiveTo").Value = ""
m4addin.itemObj.Save

'Checking for customProperty existance in saveAs copy - which should
work now
Dim foo As MailItem
Set foo = m4addin.oApp.CreateItemFromTemplate(myConfig.HDDq &
projectFilename & ".msg")
MsgBox foo.UserProperties.Count
Set foo = Nothing
 

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