Search This Blog

Saturday, September 17, 2011

Creating addIns with VSTO

Abstract => VSTO used to create custom plugins for Microsoft office and will be added to office applications to pull/insert data to it. The plugins once installed will be shown in office and will run the user code.
Let's see how to create application-level add-ins for Microsoft Office Outlook 2007 using VSTO 3.0. We will create a Hello World application-level example for Microsoft Office Outlook 2007 using Visual Studio 2008.
  1. Open Visual Studio 2008 to create a new Outlook 2007 Add-in template project.
  2. Select New Project. Under Office select 2007, and then select the Microsoft Outlook Add-in template, and name the project as per your requirements. 
  3. The solution will be created with all of the supporting files required for the development of Outlook solution, as shown in the following screenshot:
    VSTO 3.0 for Office 2007 Programming
  4. Write the following code to display a message box:
    // Windows forms namespace to display Message box
    using System.Windows.Forms;
  5. Write the code to display the Say Hello World! message while loading:
    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
    // Message box display
       MessageBox.Show("Say Hello World!");
    }
The result will be similar to what is shown in the following screenshot:

VSTO 3.0 for Office 2007 Programming
















Likewise, we can use VSTO apis to manipulate various outlook attributes. Here I will show one sample for updating/adding the outlook form regionss
Outlook forms bring you a new design with the support of .NET controls, in order to easily integrate with your Visual Studio Designer and provide code templates and debugging options.
  1. Open Visual Studio 2008, to create a new Outlook 2007 Add-in template project.
  2. Select New Project. Under Office select 2007 and select Outlook 2007 Add-in template and name the project as per your requirement.
  3. The solution will be created with all of supporting files required for the development of our Outlook solution.
  4. Right-click on your project solution and click on the Add New Item option. Select the Outlook Form Region template as shown in the following screenshot:
    VSTO 3.0 for Office 2007 Programming
  5. After clicking on the Add button in the Visual Studio 2008 Add New Item dialog box, a wizard window in which you can set the properties for New Outlook Form Region will appear as shown in the following screenshot:
    VSTO 3.0 for Office 2007 Programming
  6. A form region can be created as either a separate form or as a form adjoining the existing form, or you can also overwrite an existing form with the new form you're creating. In the New Outlook Form Region wizard, you have options to select accordingly.
    VSTO 3.0 for Office 2007 Programming
  7. Name the new Outlook form region as per your requirements:
    VSTO 3.0 for Office 2007 Programming
  8. Next, associate the standard message classes to the Outlook form region:
    VSTO 3.0 for Office 2007 Programming
  9. Once you are done creating the Outlook form region, run the solution. The new form region will appear as shown in the following screenshot:
    VSTO 3.0 for Office 2007 Programming
  10. We can also write a program to display the current contact item's email address in the form region. Open the FormRegion1.cs file and write the following code inside the FormRegionShowing event:
    // Occurs before the form region is displayed
    // Use this.OutlookItem to get a reference to the current
    Outlook item
    // Use this.OutlookFormRegion to get a reference to the
    form region
    private void FormRegion1_FormRegionShowing(object sender,
    System.EventArgs e)
    {
    // Verify the current Outlook item is Outlook contact item
    if (this.OutlookItem is Outlook.ContactItem)
    {
    // Get reference through the Outlook contact item
    Outlook.ContactItem PacktMailContactItem = (Outlook.
    ContactItem)this.OutlookItem;
    // Message box displayed while you click the form region
    button in the Ribbon
    MessageBox.Show("This form region to display: " +
    PacktMailContactItem.Email1DisplayName);
    // The listview control displays the current contacts
    display email
    ContactListView.Items.Add(PacktMailContactItem.
    Email1DisplayName.ToString());
    }
    }
The addition and execution of the preceding code results in the following screenshot:
VSTO 3.0 for Office 2007 Programming
One of the key advantages of form regions is that you can use themed controls that match the look of standard Outlook forms in Outlook 2007, and you also have easy access to new controls for date and time picking, and time zones. Form regions will help you to build custom contact forms, meeting invitees, and enhanced UI for Microsoft Office Outlook 2007. Outlook 2007 also contains a huge number of new form controls that offer the business logic and performance that users expect from Outlook forms.

  1. We can also write a program to display the current contact item's email address in the form region. Open the FormRegion1.cs file and write the following code inside the FormRegionShowing event:
    // Occurs before the form region is displayed
    // Use this.OutlookItem to get a reference to the current
    Outlook item
    // Use this.OutlookFormRegion to get a reference to the
    form region
    private void FormRegion1_FormRegionShowing(object sender,
    System.EventArgs e)
    {
    // Verify the current Outlook item is Outlook contact item
    if (this.OutlookItem is Outlook.ContactItem)
    {
    // Get reference through the Outlook contact item
    Outlook.ContactItem PacktMailContactItem = (Outlook.
    ContactItem)this.OutlookItem;
    // Message box displayed while you click the form region
    button in the Ribbon
    MessageBox.Show("This form region to display: " +
    PacktMailContactItem.Email1DisplayName);
    // The listview control displays the current contacts
    display email
    ContactListView.Items.Add(PacktMailContactItem.
    Email1DisplayName.ToString());
    }
    }
The addition and execution of the preceding code results in the following screenshot:
VSTO 3.0 for Office 2007 Programming

No comments:

Post a Comment