WebHosting

Tuesday, October 18, 2011

//namespade add(Microsoft.Office.Interop.Outlook.dll)
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 ......

Featured Posts

How to Enable and Configure Remote Desktop (RDP) on Ubuntu 24.04.4 LTS

Remote Desktop Protocol (RDP) support in Ubuntu has become considerably easier with the GNOME Remote Desktop stack included in modern Ubuntu...