Office Add-InsThe second pattern used in Office development is the add-in pattern. This book covers several types of Office add-ins. These include VSTO add-ins for Outlook, COM add-ins for Excel and Word, and automation add-ins for Excel:
This book does not discuss some Office add-in technologies. Smart Documents add-ins are not discussed because VSTO provides a much easier way of accessing Smart Document functionality, albeit at the document or template level rather than at the application level. For more information on VSTO's support for Smart Documents, see Chapter 15, "Working with Actions Pane." Creating an Outlook Add-In in VSTO
To create an Outlook add-in project in VSTO, choose Project from the New menu of the File menu in Visual Studio. Select the Visual C# node from the list of project types, and select the Office node under the Visual C# node. The Outlook add-in project appears in the list of templates. Type a
VSTO creates a project with references to the Outlook 2003 PIA, the
Figure 2-6. The Outlook add-in project in Solution Explorer.
Figure 2-5. Creating a new Outlook add-in project.
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 where you can write code that executes on the startup and shutdown of the add-in. The ThisApplication class derives from an aggregate of the Outlook Application object. This allows you to access properties and methods of the Outlook Application object by writing code such as this.Inspectors.Count in the ThisApplication class. Listing 2-4. The Initial Code in the ThisApplication Class in an Outlook Add-In Project
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)
{
}
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
}
}
Looking at Listing 2-4, you might
The InternalStartup method is generated by VSTO and used to hook up any event handlers generated by VSTO. This is where the Startup and Shutdown event handlers are hooked up. You should not edit this section of the code. We may omit this block of code in some of the listings in this book, but the block of code must be in the classotherwise, the class will fail to compile.
We are going to add to the code in Listing 2-4 to create an add-in that will solve an annoying problempeople replying inadvertently to an e-mail sent out to a mailing alias that contains a large number of people. Unless you have "Vice President" in your title, you probably do not want to be sending e-mail to more than, say, 25 people at any given time. We are going to create an add-in that will warn you if you do this and give you the "This is a
Outlook's Application object has an ItemSend event that is raised whenever a
In our ItemSend event handler, we need to check to see whether the
item
parameter which is passed as an
object
is actually an e-mail. The
Listing 2-5. A VSTO Outlook Add-In That Handles the ItemSend Event and Checks for More Than 25 Recipients
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
}
}
When you run the project with the code shown in Listing 2-4, Outlook launches and the add-in loads. Try sending a mail to an alias that includes more than 25 peopleyou might want to go offline first in case you
Chapter 24, "Creating Outlook Add-Ins with VSTO," discusses VSTO Outlook add-ins in more detail. Chapters 9 through 11"Programming Outlook," "Working with Outlook Events," and "Working with Outlook Objects," respectivelydiscuss the Outlook object model. |