I just have to do a very easy task: when a user clicks Send > E-Mail in Word 2007, I have to do some work, before the E-Mail-window appears.
The implementation is easy, as you will see in the following. I use Visual Studio 2008 for this sample.
The first thing we do, we create a new project in Visual Studio. The template we use is under Extensions > Common Add-In. The assistant asks for the target-application. We select Microsoft Word.
When the project is created, we have to add a reference to the .NET component Microsoft.Office.Interop.Word (you should select the one with version 184.108.40.206) and to System.Windows.Forms. After that we add some code to the Connect.cs in our project:
Now we have to create the xml-file for the ribbon. In our project we add a new xml-file named Ribbon.xml and add this code:
In the properties of the xml-file select this file as an embedded resource. Then open the properties of the project and select the Resources-tab. Click to create a new resource-file, then drag & drop the Ribbon.xml into the resource-window. Now it’s a gook idea to do a save-all.
Go back to the file Connect.cs and add a reference to the IRibbonExtensibility interface.
After that right-click the word IRibbonExtensibility and select Implement Interface > Implement Interface Explicitly. This adds a stub for the GetCustomUi-member.
Remove the “throw new …” line and insert a new line with
Now we can implement our own functionality for the SendAsAttachement” in the ribbon. Therefore we just add a public method with the same name, as in the onAction-attribute in the Ribbon.xml.
Now you can build your project to create the assembly. Then you have to install the Add-In. Within your project, you will find a section for the setup. In my sample it is named MySendFileSetup. Right-click this folder and select “Build”. When the build was successful, right-click again and select “Install”. Follow the setup-assistant and click <Finish> on the last page.
Open Word 2007 and click the Office-button > Send > E-Mail. Now you should see a dialog box from the Add-In.
In our code we inspect the result of this dialog. When the user clicks <OK>, the cancelDefault-variable is set to false. That means, that the default function is called after our code.
It’s not so difficult, isn’t it?