次の方法で共有


Step-by-step - How to create an Outlook 2007 addin with a form region

The information in this weblog is provided "AS IS" with no warranties, and confers no rights. This weblog does not represent the thoughts, intentions, plans or strategies of my employer. It is solely my opinion. Inappropriate comments will be deleted at the authors discretion. All code samples are provided "AS IS" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.

When using Visual Studio 2008, creating an Outlook 2007 addin is very simple. There are wizards available that takes care of most tasks so it is possible to get very far without writing a single line of code. In this walk-through I will show how to create an add-in that shows a form region shows the e-mail headers of a message. The form region will only be visible for normal messages (messages of type IPM.Note). The below screen shot shows how the form region looks like at the bottom of an e-mail message.

 

 

  1. Click File | New | Project... in Visual Studio to create a new project. Under Visual C#, expand Office, click 2007 and in the right pane click Outlook 2007 Add-in. Name the project HeaderAddin and choose the directory where the wizard will create the add-in.

  2. Right click the project name in the solution explorer window and click properties. Click Assembly information and change the language to English UK. Click OK until you see the main project window.

  3. Right click the project name in the solution explorer window and click add | new item... Under Visual C# Items choose Outlook Form Region. Name the file headerregion and click the add button. This will start the New Outlook Form Region Wizard.

  4. Make sure Design a new form region is chosen and click next.

  5. On the next wizard page choose Adjoining and click next.

  6. Name the new form region HeaderRegion. Uncheck Inspectors that are in compose mode to prevent the form region from being displayed when the user writes a new message or replies to existing messages. Leave the other two options checked.

  7. Choose Mail Message (IPM.Note) and leave all the other options unchecked and press Finish.

  8. headerregion.cs should now be shown in design mode. If it is not, double click headerregion.cs in the solution explorer to
    bring the new form region up. Resize the form region until an appropriate size. 600x150 will work well. Click View | toolbox and drag a textbox control on to the form region. Resize the textbox to the same size as the form region.

  9. Change the textbox style to multiline and make it readonly.

  10. Right click HeaderRegion.cs and choose view code. Scroll down to the method called HeaderRegion_FormRegionShowing and add the following code

    private void HeaderRegion_FormRegionShowing(object sender, System.EventArgs e)
    {
    // query for PR_TRANSPORT_MESSAGE_HEADERS here and display it in the form region.
    Outlook.MailItem item = (Outlook.MailItem)this.OutlookItem;
    String prop = "https://schemas.test.com/mapi/proptag/0x007D001E";
    Outlook.PropertyAccessor oPa = item.PropertyAccessor;
    String strHeaders = (String) oPa.GetProperty(prop);
    textBox1.Text = strHeaders;
    }

  11. Press CTRL+SHIFT+B to compile. As long as Outlook is installed on the same machine as Visual Studio, you can press CTRL+F5 to start Outlook with the form region loaded.