To create a new Outlook project in Visual Studio
- Start Visual Studio.
- On the File menu, point to New, and then click Project.
- In the templates pane, expand Visual C# or Visual Basic, and then expand Office.
- 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.
- In the list of project templates, select Outlook 2007 Add-in or Outlook 2010 Add-in.
- In the Name box, type FirstOutlookAddIn.
- 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
- 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.
- 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); }
- 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"; } } }
- 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,
No comments:
Post a Comment
Thank you for Commenting Will reply soon ......