次の方法で共有


Changes in the Add-In Model between VSTO 2.0 and VSTO 3.0

It is instructive to compare code from the previous edition of my book to see what changed between Outlook 2003 and Outlook 2007 as well as the changes to the add-in model between VSTO 2.0 and VSTO 3.0.  

This is a VSTO Outlook add-in that handles the ItemSend event and checks if there are more than 25 recipients to warn you that you are sending to a lot of people.

Here is the VSTO 2.0 and Outlook 2003 version:

 

 using System;
 using System.Windows.Forms;
 using Microsoft.VisualStudio.Tools.Applications.Runtime;
 using Outlook = Microsoft.Office.Interop.Outlook;
  
 namespace OutlookAddin1
 {
   public partial class ThisApplication
   {
     private void ThisApplication_Startup(object sender, EventArgs e)
     {
       this.ItemSend += new 
         Outlook.ApplicationEvents_11_ItemSendEventHandler(
         ThisApplication_ItemSend);
     }
  
     void ThisApplication_ItemSend(object item, ref bool cancel)
     {
       Outlook.MailItem myItem = item as Outlook.MailItem;
  
       if (myItem != null)
       {
         foreach (Outlook.Recipient recip in myItem.Recipients)
         {
           if (recip.AddressEntry.Members.Count > 25)
           {
             // Ask the user if she really wants to send this e-mail
             string message = "Send mail to {0} with {1} people?";
             string caption = "More than 25 recipients";
             MessageBoxButtons buttons = MessageBoxButtons.YesNo;
             DialogResult result;
  
             result = MessageBox.Show(String.Format(message,
               recip.AddressEntry.Name,
               recip.AddressEntry.Members.Count),
               caption, buttons);
  
             if (result == DialogResult.No)
             {
               cancel = true;
               break;
             }
           }
         }
       }
     }
  
     private void ThisApplication_Shutdown(object sender, EventArgs e)
     {
     }
  
     #region VSTO Designer generated code
     private void InternalStartup()
     {
       this.Startup += new System.
         EventHandler(ThisApplication_Startup);
       this.Shutdown += new System.
         EventHandler(ThisApplication_Shutdown);
     }
     #endregion
   }
 }

Here is the VSTO 3.0 and Outlook 2007 version:

 

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Xml.Linq;
 using Outlook = Microsoft.Office.Interop.Outlook;
 using Office = Microsoft.Office.Core;
 using System.Windows.Forms;
  
 namespace OutlookAddIn1
 {
   public partial class ThisAddIn
   {
     private void ThisAddIn_Startup(object sender,
       System.EventArgs e)
     {
       this.Application.ItemSend += new 
         Outlook.ApplicationEvents_11_ItemSendEventHandler( 
         Application_ItemSend);
     }
  
     int CountRecipients(Outlook.AddressEntry entry)
     {
       int count = 1;
       if (entry.Members == null)
         return 1;
       else
       {
         foreach (Outlook.AddressEntry e in entry.Members)
         {
           count = count + CountRecipients(e);
         }
       }
       return count;
     }
  
     void Application_ItemSend(object Item, ref bool Cancel)
     {
       Outlook.MailItem myItem = Item as Outlook.MailItem;
       int recipientCount = 0;
  
       if (myItem != null)
       {
         foreach (Outlook.Recipient recip in myItem.Recipients)
         {
           recipientCount += CountRecipients(recip.AddressEntry);
         }
  
         if (recipientCount > 25)
         {
             // Ask the user if she really wants to send this e-mail
             string message = "Send mail to {0} people?";
             string caption = "More than 25 recipients";
             MessageBoxButtons buttons = MessageBoxButtons.YesNo;
             DialogResult result;
  
             result = MessageBox.Show(String.Format(message,
               recipientCount),
               caption, buttons);
  
             if (result == DialogResult.No)
             {
               Cancel = true;
               return;
             }
           }
         }
       }
  
     #region VSTO generated code
     private void InternalStartup()
     {
       this.Startup += new System.EventHandler(ThisAddIn_Startup);
     }
     #endregion
   }
 }

What has changed?

First, the add-in class is now called ThisAddIn rather than ThisApplication and is found in the file “ThisAddIn.cs” rather than “ThisApplication.cs”. Note that this change was made in the VSTO 2005 Second edition release—a free point release we did between VSTO 2.0 and VSTO 3.0.  Because the Outlook Application object is now provided via a member variable called Application rather than as an aggregate of the base class, the event hookup code that previously started with this.ItemSend in the VSTO 2.0 add-in model is now this.Application.ItemSend.

Also interesting is one line that did not change between versions—the delegate definition ApplicationEvents_11_ItemSendEventHandler. One would think that this would have changed to be prefaced with ApplicationEvents_12 as 12 is the internal version number for Outlook 2007. But the Outlook team decided to not create a new events interface for Outlook 2007 and instead extended the existing ApplicationEvents_11 interface.

Comments