Storing the state of a VSTO Outlook plugin in a draft message

44 Views Asked by At

I try without success to store the state of a VSTO plugin in a draft e-mail. I add user properties on an MailItem.Write event (when I save the draft message) but I retrieve nothing when opening the saved draft on an MailItem.Open event.

Some code OK :

public bool Item_Write(ref bool Cancel)
{
    Cancel = false;
    string state = "OK";
    [snip]
    /* does work : "OK" seen with OutlookSpy on draft message */
    item.Categories = new string(state);
}

public bool Item_Open(ref bool Cancel)
{
    Cancel = false;
    [snip]
    /* does work : "OK" string retrieved (on debugger breakpoint) on draft double-click */
    string state = item.Categories;
}

Some code KO : no property retrieved

public bool Item_Write(ref bool Cancel)
{
    Cancel = false;
    string state = "OK";

    /* does not work */
    UserProperties userProperties = null;
    UserProperty pluginState = null;

    userProperties = item.UserProperties;
    if (userProperties != null)
    {
        pluginState = userProperties.Add("PluginState", OlUserPropertyType.olText, false, 1);
        pluginState.Value = new string(state);
    }
    
    /* does not work either */
    string proptag = "http://schemas.microsoft.com/mapi/proptag/0x003D001F";
    item.PropertyAccessor.SetProperty(proptag, new string(state)); 
}

public bool Item_Open(ref bool Cancel)
{
    Cancel = false;
    string state;

    /* UserProperties does not work : pluginState == null on draft double-click */
    UserProperties userProperties = null;
    UserProperty pluginState = null;

    userProperties = item.UserProperties;
    if (userProperties != null)
    {
        pluginState = userProperties.Find("PluginState", true);
        if (pluginState != null)
            state = pluginState.ToString();
    }
    
    /* PR_SUBJECT_PREFIX_W does not work either : state == null on draft double-click */
        string proptag = "http://schemas.microsoft.com/mapi/proptag/0x003D001F";
        state = item.PropertyAccessor.GetProperty(proptag);
}

I launch the plugin under Visual and in Outlook I just create a new mail, close it and answer yes to save the draft ...

Any help appreciated

1

There are 1 best solutions below

20
Dmitry Streblechenko On

Firstly, there is no Application.ItemWrite event. There is MailItem.Write event, but it passes Cancel (by ref) parameter, not Item. Ditto for Application.ItemOpen: there is no such event.

Try to remove a call to mailItem.Save() - your message will be saved immediately after the your event handler runs unless you set Cancel parameter to true. Try to use OutlookSpy (I am it author - click IMessage button) to make sure your custom prop is indeed saved.

When reading the custom prop, since there is no Application.ItemOpen event, you can use Application.Inspectors.NewInspector event or Application.ActiveExplorer.SelectionChange event. Application.ItemLoad event is not a good option - it fires too often.

Keep in mind that a user prop can cause Outlook to send in the RTF (TNEF) format - to make sure that does not happen, use MailItem.PropertyAccessor.SetProperty/GetProperty. You can use the same DASL property as the one used your your custom property (see the DASL name in OutlookSpy).