views:

22

answers:

2

In Outlook, I can set the subject for a new message (when composing a new mail message), but I want to prepend text. So I need to get the subject first, and then set it.

Outlook.Application application = Globals.ThisAddIn.Application;
Outlook.Inspector inspector = application.ActiveInspector();
Outlook.MailItem myMailItem = (Outlook.MailItem)inspector.CurrentItem;

if (myMailItem != null && !string.IsNullOrEmpty(myMailItem.Subject))
{
    myMailItem.Subject = "Following up on your order";
}

This code works on replies, but not for new messages, because in that case, myMailItem is null.

A: 

CurrentItem is for the current email item.

You need to create a new one.

Outlook.MailItem mic = (Outlook.MailItem)(application.CreateItem(Outlook.OlItemType.olMailItem)); 
Dan Vallejo
A: 

This is what I was looking for:

if (thisMailItem != null)
{
    thisMailItem.Save();

    if (thisMailItem.EntryID != null)
    {
        thisMailItem.Subject = "prepended text: " + thisMailItem.Subject;
        thisMailItem.Send();
    }
}

The subject was null until the mail item had been saved, either because it was sent, or as a draft. We can save it programmatically and then get the subject.

One other note: if the subject is blank at the time of saving, it will still show as null.

vwfreak034