Search code examples
c#outlookvstooutlook-addinemail-attachments

Outlook VSTO: Renaming existing Attachments does not show the new name


I'm trying to rename existing Attachments (change the DisplayName) in Outlook with VSTO.

After the rename occurs, the new name is not visible in the currently open Window.

However, when a different Folder or Email is selected and then the original Email is selected again, then the Attachments are correctly shown as being renamed.

This seems to me to be some sort of "refresh" issue.

public void RenameAttachment(Office.IRibbonControl control)
    {
        Outlook.Attachment attachment = null;
        Outlook.AttachmentSelection selection = null;

        try
        {
            selection = control.Context as Outlook.AttachmentSelection;
            if (selection != null)
            {
                attachment = selection[1];

                if (attachment != null)
                {
                    attachment.DisplayName = "NewName12345"; //Sample
                }
            }
        }
        finally
        {
            Marshal.ReleaseComObject(attachment);
            Marshal.ReleaseComObject(selection);
            Marshal.ReleaseComObject(control.Context);
            Marshal.ReleaseComObject(control);
        }
    }

Do I need to perform a manual refresh?

If so, how do I refresh the current displayed Window so the User sees the new name of Attachments?


Solution

  • I tried all the mentioned solutions, but nothing completely solved the issue for me. I resolved it with a workaround.

    1. Download the attachment to disk
    2. Remove the attachment from the MailItem
    3. Commit changes (MailItem.Save)
    4. Add the downloaded attachment with the desired new name
    5. Commit changes (MailItem.Save)

    This provides the functionality I want and makes sure that users do not have to refresh the email (navigate to another Folder and then select the Email again).