Presently sponsored by: ScriptRunner - Get your free PowerShell Security e-Book!
In this Visual Studio Tools for Office I will walk you through how to make a very simple customization to the Word 2007 (or any of these: Visio, PowerPoint, InfoPath, Outlook or Excel) Ribbon-menu and add our own button with some code to execute when it’s pressed.
Basically, what you need to do before doing anything else is to install and configure the VSTO add-in for Visual Studio. Just search www.microsoft.com/downloads for it and you will find it.
Once the prerequisites are installed, we will create a new project and choose the Word-template as seen in this screenshot:
What we do next before doing anything else, is the following: Right-click your project in the Solution Explorer, choose "Add" -> "New Item…" and choose the Ribbon component, as seen in the following Screenshot:
Once that’s done. Uncomment the commented code in the new Ribbon1.cs (or whatever you chose to call it).
Basically, everything’s done now. All we need to do is add some buttons or any other controls we would like to add in order to extend the Ribbon-menu.
In the following screenshot I’ve done only minor modifications to the labels and text and I havn’t added any other controls, to keep it simple:
Note: I’m defining a method to be executed when the button is toggeled with this xml: onAction="OnToggleButton1"
The following code shows a simple method that’s implemented by default with some minor modifications to insert my signature when the button is toggeled (Best practice would be to use the button control, not the toggleButton.. I just chose to do it this way because I couldn’t care less to change the default ;)
We’re done for now.
Execute the project and you’ll see the following screen with the option to press a toggleButton that will insert my very basic signature into the document:
What I wanted to show with this blogpost was simply the fact that it’s quite easy to extend the Ribbon to enable the use of your own custom code. The possibilites are almost endless since you get access to the entire .NET Framework via the code and can do basically whatever you want.
You can also integrate the menu with SharePoint via the Microsoft.SharePoint namespace, however if you’re going to communicate with the SharePoint API I would suggest making a WebService with the required methods and host it on the SharePoint-machine and then call the WebService from your clients.
Note: In order for this to work on the client machines, I think you need to install the VSTO on them aswell (Note, a full installation of the Office-suite will enable this feature if I’m not mistaken). The clients will also need the .NET Framework installed.
That’s it for now. I hope someone could enjoy this simple VSTO Tip :)