Click here to Skip to main content
Click here to Skip to main content

Rename Attachments in Outlook

, 7 Apr 2013
Rate this:
Please Sign up or sign in to vote.
Rename attachments before sending any email

Introduction

To be honest, I created this solution to solve my problems and I hope it solves yours too. I couldn't find something like this while I searched online for long.

Sometimes, we need to forward an email which has an attachment. Sometimes, we create a new document and email it directly from File menu as attachment. In these cases, the file name may not be what we would like to send as (Document1.docx, Book1.xlsx, Powerpoint1.pptx, etc.). Sometimes, the date in the file name is incorrect or there is a spell error.

Outlook or any other tool in the internet (as far as I could search) doesn't solve this problem. So, I created a simple tool using C# to allow me to edit the file name on the fly.

Background

I was hoping that someone would have created this tool already but couldn't find it anywhere... Thus I prepared it myself.

Using the Code

Ideally, the best way to handle this would have been to edit the context menu tool button that said "Rename Attachment". Clicking the button should have saved the file in a temporary directory, renamed it and then should have reattached it. The original file should have been deleted.

However, I wasn't successful in adding the button to the right click menu and hence I chose a different approach. I used a window form to display whenever a mail was sent. This form offers an option to rename attachments.

The approach was is as below:

  1. Catch any mail going out.
  2. List all attachments. Filter out the extension and offer file name for renaming.
  3. For easy renaming, create textboxes on the fly for each attachment.
  4. Once clicked OK, save the file as new filename, re - attach the email, delete the old attachment and send the mail.
  5. Use a timer to ticket after a while and delete this temporary file also.

The key piece of the code is below:

//Handle itemsend event to handle mail sent event.
private void ThisAddIn_Startup(object sender, EventArgs e)
{
      Application.ItemSend += 
       new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler
       (Application_ItemSend);
}

//capture any mail going out
void Application_ItemSend(object Item, ref bool Cancel)
{
      //Use mail item as mail captured
      Microsoft.Office.Interop.Outlook.MailItem mailItem = 
      	Item as Microsoft.Office.Interop.Outlook.MailItem;

      if (mailItem != null)
      {
        var attachments = mailItem.Attachments;

        if (attachments.Count == 0)
          return;

        RenameForm form = new RenameForm();
        //Add textbox for each attachment
        Dictionary<int, Microsoft.Office.Interop.Outlook.Attachment> 
        storedAttachments = new Dictionary<int, Microsoft.Office.Interop.Outlook.Attachment>();
        int i = 0;

        foreach (Microsoft.Office.Interop.Outlook.Attachment attachment in attachments)
        {
          i++;
          form.AddAttachmentDetail(i, attachment);
          storedAttachments.Add(i, attachment);
        }

        DialogResult result = form.ShowDialog();

        if (result == DialogResult.OK)
        {
          string tempFolder = Environment.GetFolderPath
               (Environment.SpecialFolder.LocalApplicationData);
          if (!tempFolder.EndsWith("\\"))
            tempFolder += "\\";

          List<string> filesToDelete = new List<string>();

          int j = 1;

          foreach (var keyValue in form.RenameRequired)
          {
            if (keyValue.Value)
            {
              storedAttachments[keyValue.Key].SaveAsFile(tempFolder + form.NewNames[keyValue.Key]);
              mailItem.Attachments.Remove(j);
              j--;
            }

            j++;
          }

          foreach (var keyValue in form.RenameRequired)
          {
            if (keyValue.Value)
            {
              mailItem.Attachments.Add(tempFolder + form.NewNames[keyValue.Key]);
              filesToDelete.Add(tempFolder + form.NewNames[keyValue.Key]);
            }
          }

          Timer timer = new Timer();
          timer.Interval = 120000;
          timer.Tick += timer_Tick;
          timer.Tag = filesToDelete;
          timer.Start();
        }
        else if (result == DialogResult.Abort)
        {
          Cancel = true;
        }
      }
}

In order to use this tool in Outlook 2010 (I have tested only this version), we need to install the following at least:

  1. .NET 4
  2. Microsoft Office 2010: Primary Interop Assemblies Redistributable
  3. Visual Studio Tools for Office Runtime

Enjoy!

Points of Interest

It is not a very clever piece of code and I am sure that there are improvements possible to use... But I was hoping to achieve the results first and I am sure that new versions are possible. The idea was that these things are possible in Outlook and it is only a matter of looking at things differently.

My next interest is to create a project management tool through Outlook programming.

History

  • Version 1

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Som Shekhar

India India
No Biography provided

Comments and Discussions

 
QuestionHow can we change, read and edit the excel or word attachment Pinmembervarunmaggo11-Nov-13 22:30 
AnswerRe: How can we change, read and edit the excel or word attachment PinmemberSom Shekhar16-Dec-13 19:30 
QuestionIs the source download not working? PinmemberYanaung77714-Jul-13 17:18 
AnswerRe: Is the source download not working? PinmemberSom Shekhar16-Jul-13 6:08 
GeneralRe: Is the source download not working? PinmemberMember 1015709817-Jul-13 14:30 
GeneralRe: Is the source download not working? PinmemberSom Shekhar19-Jul-13 8:56 
GeneralRe: Is the source download not working? PinmemberSom Shekhar16-Dec-13 19:32 
AnswerRe: Is the source download not working? PinmemberSom Shekhar16-Dec-13 19:32 
SuggestionNipping the problem in the bud Pinmembermrchief_20009-Apr-13 6:28 
GeneralRe: Nipping the problem in the bud PinmemberSom Shekhar9-Apr-13 7:11 
QuestionNot an article PinmvpOriginalGriff14-Dec-12 8:23 
AnswerRe: Not an article PinmemberSom Shekhar14-Dec-12 15:25 
AnswerRe: Not an article PinmemberSom Shekhar14-Dec-12 15:27 
GeneralRe: Not an article Pinmvp_ Kunal Chowdhury _15-Dec-12 20:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 8 Apr 2013
Article Copyright 2012 by Som Shekhar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid