Click here to Skip to main content
15,795,318 members
Articles / Productivity Apps and Services / Microsoft Office
Tip/Trick

Rename Attachments in Outlook

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
7 Apr 2013CPOL2 min read 40.8K   484   5   19
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.

Image 1

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:

C#
//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)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionRename Attachments Pin
Greg Peck4-Aug-21 20:51
Greg Peck4-Aug-21 20:51 
AnswerRe: Rename Attachments Pin
Som Shekhar6-Oct-22 7:00
Som Shekhar6-Oct-22 7:00 
QuestionGetting Installation Error Due To Certificate Pin
Member 121076132-Nov-15 8:36
Member 121076132-Nov-15 8:36 
AnswerRe: Getting Installation Error Due To Certificate Pin
Som Shekhar3-Aug-16 22:50
Som Shekhar3-Aug-16 22:50 
GeneralRe: Getting Installation Error Due To Certificate Pin
Member 128819711-Dec-16 12:49
Member 128819711-Dec-16 12:49 
QuestionHow can we change, read and edit the excel or word attachment Pin
varunmaggo11-Nov-13 23:30
varunmaggo11-Nov-13 23:30 
AnswerRe: How can we change, read and edit the excel or word attachment Pin
Som Shekhar16-Dec-13 20:30
Som Shekhar16-Dec-13 20:30 
I think outlook allows you to do that as an inbuilt functionality.

You can simply open a file, make the changes and save.

Usually, I prefer forwarding the mail and then do these activities so that the new file is saved in the forwarded mail and not in the original mail.
Som

QuestionIs the source download not working? Pin
Yanaung77714-Jul-13 18:18
professionalYanaung77714-Jul-13 18:18 
AnswerRe: Is the source download not working? Pin
Som Shekhar16-Jul-13 7:08
Som Shekhar16-Jul-13 7:08 
GeneralRe: Is the source download not working? Pin
Member 1015709817-Jul-13 15:30
Member 1015709817-Jul-13 15:30 
GeneralRe: Is the source download not working? Pin
Som Shekhar19-Jul-13 9:56
Som Shekhar19-Jul-13 9:56 
GeneralRe: Is the source download not working? Pin
Som Shekhar16-Dec-13 20:32
Som Shekhar16-Dec-13 20:32 
AnswerRe: Is the source download not working? Pin
Som Shekhar16-Dec-13 20:32
Som Shekhar16-Dec-13 20:32 
SuggestionNipping the problem in the bud Pin
mrchief_20009-Apr-13 7:28
mrchief_20009-Apr-13 7:28 
GeneralRe: Nipping the problem in the bud Pin
Som Shekhar9-Apr-13 8:11
Som Shekhar9-Apr-13 8:11 
QuestionNot an article Pin
OriginalGriff14-Dec-12 9:23
mvaOriginalGriff14-Dec-12 9:23 
AnswerRe: Not an article Pin
Som Shekhar14-Dec-12 16:25
Som Shekhar14-Dec-12 16:25 
AnswerRe: Not an article Pin
Som Shekhar14-Dec-12 16:27
Som Shekhar14-Dec-12 16:27 
GeneralRe: Not an article Pin
Kunal Chowdhury «IN»15-Dec-12 21:12
professionalKunal Chowdhury «IN»15-Dec-12 21:12 

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

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