Showing posts with label createing outlook plugin. Show all posts
Showing posts with label createing outlook plugin. Show all posts

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
}
}

Wednesday, October 5, 2011

Creating Application-Level Add-in for Outlook

To create a new Outlook project in Visual Studio

  1. Start Visual Studio.
  2. On the File menu, point to New, and then click Project.
  3. In the templates pane, expand Visual C# or Visual Basic, and then expand Office.
  4. Under the expanded Office node, select the 2007 if you have Outlook 2007 installed, or select the 2010 node if you have Outlook 2010 installed.
  5. In the list of project templates, select Outlook 2007 Add-in or Outlook 2010 Add-in.
  6. In the Name box, type FirstOutlookAddIn.
  7. Click OK.
    Visual Studio creates the FirstOutlookAddIn project and opens the ThisAddIn code file in the editor.


To add text to the subject and body of each new mail message

  1. In the ThisAddIn code file, declare a field named inspectors in the ThisAddIn class. The inspectors field maintains a reference to the collection of Inspector windows in the current Outlook instance. This reference prevents the garbage collector from freeing the memory that contains the event handler for the NewInspector event.
    C#
    Outlook.Inspectors inspectors;
    
    
    
  2. Replace the ThisAddIn_Startup method with the following code. This code attaches an event handler to the NewInspector event.
    C#
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        inspectors = this.Application.Inspectors;
        inspectors.NewInspector +=
        new Microsoft.Office.Interop.Outlook.
    InspectorsEvents_NewInspectorEventHandler(Inspectors_NewInspector);
    }
    
    
    
  3. In the ThisAddIn code file, add the following code to the ThisAddIn class. This code defines an event handler for the NewInspector event.
    When the user creates a new mail message, this event handler adds text to the subject line and body of the message.
    C#
    void Inspectors_NewInspector
    (Microsoft.Office.Interop.Outlook.Inspector Inspector)
    {
        Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem;
        if (mailItem != null)
        {
            if (mailItem.EntryID == null)
            {
                mailItem.Subject = "text was added by using code";
                mailItem.Body = "text was added by using code";
            }
    
        }
    }
    
    
    
To modify each new mail message, the previous code examples use the following objects:
  • The Application field of the ThisAddIn class. The Application field returns an Application object, which represents the current instance of Outlook.
  • The Inspector parameter of the event handler for the NewInspector event. The Inspector parameter is an Inspector object, which represents the Inspector window of the new mail message. For more information,







Featured Posts

Kali Linux Remote Desktop: Access GNOME from Windows Using Native RDP

  Kali Linux + GNOME 50 + GNOME Remote Desktop + Windows Remote Desktop (MSTSC) Getting a full GNOME desktop remotely on Kali Linux can be ...