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:
- Catch any mail going out.
- List all attachments. Filter out the extension and offer file name for renaming.
- For easy renaming, create textboxes on the fly for each attachment.
- Once clicked OK, save the file as new filename, re - attach the email, delete the old attachment and send the mail.
- Use a timer to ticket after a while and delete this temporary file also.
The key piece of the code is below:
private void ThisAddIn_Startup(object sender, EventArgs e)
{
Application.ItemSend +=
new Microsoft.Office.Interop.Outlook.ApplicationEvents_11_ItemSendEventHandler
(Application_ItemSend);
}
void Application_ItemSend(object Item, ref bool Cancel)
{
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();
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:
- .NET 4
- Microsoft Office 2010: Primary Interop Assemblies Redistributable
- 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