Office Add-Ons
The second pattern used in office development is the add-in mode. This book covers several office add-ons. These include the VSTO add-ins for Outlook, the COM add-ins for Excel and Word, and the Automation Add-ins for Excel:
VSTO add-ons for Outlook This new VSTO feature makes it easy to create add-ons for Outlook 2003. This model is the most ". NET" feature in all add-in models, and is very similar to the VSTO code behind the model file. The 24th chapter, "Creating an Outlook Add-in with VSTO," describes the model in detail.
The C # classes in the Excel and Word COM add-in class library projects can implement the IDTExtensibility2 interface and register as COM objects and COM add-ins in the registry. With COM interop, Office creates and communicates with the C # class. The 23rd chapter, "Developing COM add-ins for Word and Excel," describes the creation of COM add-ins and some issues that make COM add-in development a problem.
Automation Add-ons for Excel these managed classes expose common functions that Excel can use in formulas. C # classes must be registered as COM objects in the registry. With COM interop, Excel can create automation Add-ons and use their public methods in formulas. The automation plug-in and its use in Excel formulas are discussed in the 3rd Chapter, "Programming Excel."
This book does not discuss some of the office add-in technologies. Smart document add-ons are not discussed because VSTO provides a simpler way to access the smart document functionality, although at the document or template level instead of at the application level. For more information about VSTO support for smart documents, see Chapter 15th, "Using the action pane."
To create an Outlook add-in in VSTO
To create an Outlook load project in VSTO, choose Project from the new menu on the File menu in Visual Studio. Select the Visual C # node from the project type list, and then select the Office node under the Visual C # node. The Outlook load item appears in the list of templates. Type the name of the new Outlook load project, and select the location of the project. Then click the OK button.
VSTO creates a project that references the Outlook 2003 PIA, the core Office PIA, and other required references, as shown in 2-6. VSTO also adds the project project to the project named ThisApplication.cs. This project project contains a C # class that you will add when you implement an Outlook add-in.
If you double-click the ThisApplication.cs project item, you will see the code shown in Listing 2-4. There is a simple startup and Shutdown event handler in which you can write code that executes when an add-in is started and closed. The ThisApplication class derives from an aggregation of Outlook application objects. This allows you to access the properties and methods of Outlook application objects by writing code such as This.Inspectors.Count in the ThisApplication class.
Manifest 2-4 The initial code in this application class in Outlook load Project
usingSystem;usingSystem.Windows.Forms;usingMicrosoft.VisualStudio.Tools.Applications.Runtime;usingOutlook =Microsoft.Office.Interop.Outlook;namespaceoutlookaddin1{ Public Partial classThisApplication {Private voidThisApplication_Startup (Objectsender, EventArgs e) { } Private voidThisapplication_shutdown (Objectsender, EventArgs e) { } #regionVSTO Designer Generated codePrivate voidInternalstartup () { This. Startup + =NewSystem. EventHandler (ThisApplication_Startup); This. Shutdown + =NewSystem. EventHandler (Thisapplication_shutdown); } #endregion }}
Take a look at listing 2-4 you might think of using partial in a class definition
VSTO uses a partial class, which is. NET is a new feature that allows you to define one category in one file and one section to define another as a class. VSTO uses this feature to hide some of the other generated code associated with the ThisApplication class to reduce the complexity of the classes that write code. The final ThisApplication class compiles from some of the classes in Listing 2-4 and hides the additional code in some of the classes generated by VSTO.
The Internalstartup method is generated by VSTO and is used to connect any event handlers that are generated by VSTO. This is where the event handler hooks are started and closed. You should not edit this piece of code. We may omit this code block from certain lists in this book, but the code block must be in the hierarchy and the class will not compile.
We'll add the code in listing 2-4 to create an add-on, which will fix an annoying problem that inadvertently replies to an e-mail message that is sent to a mail alias that contains a large number of people. Unless you have a "VP" in your title, you may not want to send email to more than 25 people at any time. We will create an add-on that, if you do, will alert you and provide "this is a potential occupational restriction." Are you sure you want to send this email to 25,000 people? Information
Outlook's Application object has a ItemSend event that is generated whenever a user sends an e-mail message. We will add additional code for the startup method of the ThisApplication class to connect the event handlers for the ItemSend event, as shown in Listing 2-5. Because the ThisApplication class originates from the aggregation of Outlook application objects, we can write this. ItemSend code, because ItemSend is an event raised by the ThisApplication base class. The ItemSend event handler accepts an object parameter named Item, which is the Outlook item to send. Because a project can be any of many things, such as a meeting request or an e-mail message, the item is passed as an object rather than a specific type. The ItemSend event handler also has a bool parameter passed by reference, which is called Cancel and can be set to True to prevent Outlook items from being sent.
In our ItemSend event handler, we need to check whether the item parameter passed as an object is actually an e-mail message. The simplest way to do this is to use the AS keyword to try to convert the item parameter to Outlook.MailItem. If the conversion succeeds, the result value will be a non-null value, and we will know that the item being sent is Outlook.MailItem and therefore an e-mail message. We can then traverse the collection of recipients on the MailItem object and check whether to send to any recipient list that contains more than 25 people. Each recipient object in the recipient collection has the AddressEntry property that returns the AddressEntry object. The AddressEntry object has a members property that returns a collection that can check the count. If we find that the count is more than 25, we will display a dialog box and ask the user if they really want to send the message. If the user clicks the No button, we will set the Cancel parameter of the ItemSend event to True to cancel the sending of the occupational restriction e-mail.
Listing 2-5 a VSTO Outlook add-in that handles the ItemSend event and checks for more than 25 recipients
usingSystem;usingSystem.Windows.Forms;usingMicrosoft.VisualStudio.Tools.Applications.Runtime;usingOutlook =Microsoft.Office.Interop.Outlook;namespaceoutlookaddin1{ Public Partial classThisApplication {Private voidThisApplication_Startup (Objectsender, EventArgs e) { This. ItemSend + =NewOutlook.applicationevents_11_itemsendeventhandler (thisapplication_itemsend); } voidThisapplication_itemsend (ObjectItemref BOOLcancel) {Outlook.MailItem myitem= Item asOutlook.MailItem; if(MyItem! =NULL) { foreach(Outlook.recipient Recipinchmyitem.recipients) {if(Recip. AddressEntry.Members.Count > -) { //Ask The user if she really wants to send this e-mail stringMessage ="Send mail to {0} with {1} people?"; stringCaption ="More than 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 voidThisapplication_shutdown (Objectsender, EventArgs e) { } #regionVSTO Designer Generated codePrivate voidInternalstartup () { This. Startup + =NewSystem. EventHandler (ThisApplication_Startup); This. Shutdown + =NewSystem. EventHandler (Thisapplication_shutdown); } #endregion }}
When you run the project by using the code that is shown in listing 2-4, Outlook starts the add-in load. If you try to send a message with an alias of more than 25 people, you may want to be offline first in case you mistakenly enter the code. If all features are correct, the add-in displays a dialog box that warns you to send an e-mail message to more than 25 people, and you will be able to cancel sending e-mail. Exit Outlook to end the debugging session.
The 24th chapter, "Creating an Outlook Add-in with VSTO," discusses VSTO Outlook add-ins in more detail. The 9th to 11th chapter, "Programming Outlook", "Using Outlook Events" and "Using Outlook Objects" discusses the Outlook object model separately.
VSTO: Using C # to develop Excel, Word "8"