using Outlook = Microsoft.Office.Interop.Outlook;
//code start
namespace ModifyOutlookAttachment
{
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
void Application_ItemSend(object Item, ref bool Cancel)
{
Outlook.MailItem mailItem = Item as Outlook.MailItem;
if (mailItem != null)
{
var attachments = mailItem.Attachments;
// If the attachment a text file, convert its text to all uppercase.
foreach (Outlook.Attachment attachment in attachments)
{
ConvertOutolookAttachmentsToUppar(attachment, mailItem);
}
}
}
//will change all attachments to upper case
private void ConvertOutolookAttachmentsToUppar(Outlook.Attachment attachment, Outlook.MailItem mailItem)
{
const string PR_ATTACH_DATA_BIN =
"http://schemas.microsoft.com/mapi/proptag/0x37010102";
// Confirm that the attachment is a text file.
if (System.IO.Path.GetExtension(attachment.FileName) == ".txt")
{
// There are other heuristics you could use to determine whether the
// the attachment is a text file. For now, keep it simple: Only
// run this code for *.txt.
// Retrieve the attachment as an array of bytes.
var attachmentData =attachment.PropertyAccessor.GetProperty(PR_ATTACH_DATA_BIN);
// Convert the byte array into a Unicode string.
string data = System.Text.Encoding.Unicode.GetString(attachmentData);
// Convert to upper case.
data = data.ToUpper();
// Convert the data back to an array of bytes.
attachmentData = System.Text.Encoding.Unicode.GetBytes(data);
//Set PR_ATTACH_DATA_BIN to attachmentData.
attachment.PropertyAccessor.SetProperty(PR_ATTACH_DATA_BIN,
attachmentData);
}
}
#region VSTO generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
No comments:
Post a Comment
Thank you for Commenting Will reply soon ......