AndyJump

This has been driving me nuts :/

I have this method (SetArchived):

Code Snippet

public static void SetArchived(MSOutlook.MailItem mailItem, bool value)

{

if (mailItem.UserProperties[ARCHIVEDPROPERTYNAME] == null)

{

mailItem.UserProperties.Add(ARCHIVEDPROPERTYNAME, MSOutlook.OlUserPropertyType.olYesNo, false, MSOutlook.OlUserPropertyType.olYesNo);

mailItem.UserProperties[ARCHIVEDPROPERTYNAME].Value = value;

mailItem.Save();

}

else if (((bool)mailItem.UserProperties[ARCHIVEDPROPERTYNAME].Value) != value)

{

mailItem.UserProperties[ARCHIVEDPROPERTYNAME].Value = value;

mailItem.Save();

}

}

Which should, if needed, create a user property and then, again if needed, set the value.

I'm using this code to test if the value is true or false to find which items need archiving. If the property doesn't exist then it'll create it with a value of false:

Code Snippet

if (mailItem.UserProperties[ARCHIVEDPROPERTYNAME] == null)

SetArchived(mailItem, false);

bool isArchived = (((bool)mailItem.UserProperties[ARCHIVEDPROPERTYNAME].Value) == true)

After the archiving process I'm setting the property to true:

Code Snippet
Utilities
.SetArchived(itemToArchive, true);

But next time when I check the value it's always false again. Am I missing something obvious



Re: Visual Studio Tools for Office C# UserProperties

Phil Hoff - MSFT

Have you tried setting the AddToFolderFields flag when you add the UserProperty

Code Snippet

mailItem.UserProperties.Add(ARCHIVEDPROPERTYNAME, MSOutlook.OlUserPropertyType.olYesNo, true, MSOutlook.OlUserPropertyType.olYesNo);

-Phil




Re: Visual Studio Tools for Office C# UserProperties

AndyJump

I tried that but it then gave me an error about "MSOutlook.OlUserPropertyType.olYesNo" being an invalid DisplayFormat. I did a little more diggign around and replaced "MSOutlook.OlUserPropertyType.olYesNo" with "System.Reflection.Missing.Value" but it still didn't save the property.

I finally tried switching from "UserProperties" to "ItemProperties" and that did work. The final code is:

Code Snippet

if (mailItem.ItemProperties[ARCHIVEDPROPERTYNAME] == null)

{

mailItem.ItemProperties.Add(ARCHIVEDPROPERTYNAME, MSOutlook.OlUserPropertyType.olYesNo, true, Missing.Value);

mailItem.ItemProperties[ARCHIVEDPROPERTYNAME].Value = value;

mailItem.Save();

}

else if (((bool)mailItem.ItemProperties[ARCHIVEDPROPERTYNAME].Value) != value)

{

mailItem.ItemProperties[ARCHIVEDPROPERTYNAME].Value = value;

mailItem.Save();

}





Re: Visual Studio Tools for Office C# UserProperties

AndyJump

Ahhh ok it's not working :/ If I wait a few minutes or restart Outlook then it forgets the properties again Sad

Does anyone have a simple block of code for creating and setting a custom property on an email in an outlook addin that definatly works, preferably in c#

Edit: it appears that the property is there but it's value isn't being saved. And sometimes it works on one folder and not another :\





Re: Visual Studio Tools for Office C# UserProperties

Mike Walker

Hi

I have a confession that I used VB to get around some quirky isses as the late binding support allows things to be "got around". I did try and do this but it is sometimes painful. There are also other things to consider on Exchange Server as SP2 of Exchange 2003 has but a limit in the number of field objects available to a store that is configurable by a Reg Key but now setup with a lower ceiling than the maximum available. If you have this issue you will see this being raised in the Event log on the Exchange Server.

Another option is to use CDO but this is being depricated and is not officially supported by MS in Managed world but ironically if you wrote a VB6 app to wrapper the CDO COM Classes and then talked to the VB6 application through COM this would be... Go figure Smile

Good luck

Regards






Re: Visual Studio Tools for Office C# UserProperties

Eric Legault

What is the actual name of the custom property you are trying to save I'm a little puzzled as to how you said this works with ItemProperties - those are default fields - and this is supposed to be a custom property

Also, is this code being called during any particular Outlook event

Eric Legault

MVP - Outlook





Re: Visual Studio Tools for Office C# UserProperties

AndyJump

I've created a command bar and this code is being run as part of what happens when a button on it is clicked.

It kinda works with ItemProperties ... it has an add command and according to the documentation (which I can't seem to find right now) it works just like UserProperties. It just ItemProperties will return all properties rather than just the custom ones. I say kinda works because whilst checking the value immediatly after setting it was ok if i waited a minute or restarted outlook it'd lose the value again.

I've switched back to UserProperties now but it's still not consistantly holding the value.

The name is 'FoldingSpaceArchived'.





Re: Visual Studio Tools for Office C# UserProperties

AndyJump

I've also been asking around over on outlookcode.com and this is the response I got from

I've seen several reports of this issue, but no clues as to the cause and solution.

http://www.outlookcode.com/threads.aspx forumid=5&messageid=23539

Well atleast it's confirmation.





Re: Visual Studio Tools for Office C# UserProperties

AndyJump

I've just cut my code down to the bare minimum (i.e. click button and loop through messages setting the property) and it works fine :/

I added the bit of threading I was doing back in and that works fine too Surprise

I then thought that maybe when I MailItem.SaveAs it was then pointed the msg file rather than still in outlook. But after adding "mailItem.SaveAs(@"C:\messages\" + mailItem.EntryID + ".msg", 3);" before I set the property it's still working X(

I'm going to try rebuilding the addin completely for search and see what happens. This is certainly an odd one.





Re: Visual Studio Tools for Office C# UserProperties

Sue Mosher - Outlook MVP

This may be a long shot, but Outlook MVP Ken Slovak suggests that you try passing that
mailitem ByRef:

public static void SetArchived(ref MSOutlook.MailItem mailItem, bool
value)

See if that works. In the call to SetArchived he'd use ref also:

SetArchived(ref mail, value);




Re: Visual Studio Tools for Office C# UserProperties

Kulvinder

Hi Sue,

With due respect, will it really matter if you pass it by writing "ref" explicitly I mean in .NET, the class objects are by default passed as by reference.

I had also written the same problem on Outlook code.com http://outlookcode.com/threads.aspx forumid=5&messageid=23460

Is there any other way I mean on one machine the same code is exceuting fine and on the other machine, the properties are not getting saved at all.

Regards

Kulvinder Singh






Re: Visual Studio Tools for Office C# UserProperties

AndyJump

Well I tried rebuilding the addin again and it's still not working. Stranger still I went back to my test addin I quickly knocked up this morning, that was working, and now it wont even load.

It's saying it couldn't load the assembly, which seeing as I haven't touched it since this morning it's making VSTO for Outlook addins look very unreliable.

What I'm wanting to do is save all emails as msg files at a predefined location.

Extended MAPI is an option but I'd rather not go native if I have any choice in the matter.

Would this be possible to knock up in VBA instead Will that allow me to show a config form for setting the archive location Can I add an option to the context menu on folders





Re: Visual Studio Tools for Office C# UserProperties

AndyJump

Right I'm pretty sure now that this code is working:

Code Snippet

private void Archive(MSOutlook.MAPIFolder rootFolder)

{

foreach (MSOutlook.MAPIFolder subFolder in rootFolder.Folders)

{

Archive(subFolder);

}

foreach (object itemObject in rootFolder.Items)

{

MSOutlook.MailItem mailItem = itemObject as MSOutlook.MailItem;

if (mailItem != null && mailItem.Class == MSOutlook.OlObjectClass.olMail)

{

//Code for setting custom property

if (mailItem.UserProperties["FSArchived"] == null)

{

MSOutlook.UserProperty prop = mailItem.UserProperties.Add("FSArchived", MSOutlook.OlUserPropertyType.olText, true, Missing.Value);

prop.Value = "boo";

mailItem.Save();

mailItem.SaveAs(@"C:\messages\" + mailItem.EntryID + ".msg", 3);

}

else if (((string)mailItem.UserProperties["FSArchived"].Value) != "boo")

{

mailItem.UserProperties["FSArchived"].Value = "boo";

mailItem.Save();

mailItem.SaveAs(@"C:\messages\" + mailItem.EntryID + ".msg", 3);

}

//if (mailItem.UserProperties["FSArchived"] != null)

//{

// mailItem.UserProperties["FSArchived"].Delete();

// mailItem.Save();

//}

}

}

}

Orignally I was enumerating through each folder and creating a collection of MailItems to work with ... then using them from that collection. I was also calling SaveAs before setting the property.

Now I'm working directly with the collections provided by Outlook and calling SaveAs after setting the property.





Re: Visual Studio Tools for Office C# UserProperties

Sue Mosher - Outlook MVP

I'm just passing along Ken's suggestion. I've seen 4-5 instances of this problem, and his is the first new idea concerning it.



Re: Visual Studio Tools for Office C# UserProperties

Sue Mosher - Outlook MVP

I wonder if the problem was related to items in the collection somehow going out of scope. Seems unlikely, but given that the collection is the factor that makes a difference, worth investigating.