Fluent UI, or the Ribbon, was introduced as part of Office 2007 as a replacement of the previous system of toolbars and menus. The Fluent UI technology, like the Open XML formats, is based on xml, which allows for a much richer extensibility story for developers. In Erika Ehril's blog post she described several tools and resources related to Fluent UI extensibility. I wanted to take this opportunity to extend her post by showing you how the Open XML SDK can be used to extend or actually control custom UI within documents.
As is the case with other features in Open XML files, the Open XML SDK 2.0 for Microsoft Office supports Fluent UI through strongly typed access to the custom UI xml part as well strongly typed access to the underlying xml contained within the custom UI xml part. In other words, you can easily add, remove or modify custom UI for a particular document or set of documents using the SDK. In today's post, I am going to show you how to add custom UI to a set of documents within a directory.
If you want to jump straight into the code, feel free to download this solution here.
Imagine a scenario where I would like to programmatically add custom UI to a set of documents based on custom UI within a specific template. These documents can exist within a directory or a SharePoint library. In this scenario, my template is going to contain the same custom UI as designed by Frank Rice in his Office developer resources ribbon UI addin blog post. For the sake of this example, let's say I am starting with the following Word document as my template:
This custom UI provides a tab called Office Developer Resources, which contains commands that take you to specific web sites. The code behind these commands is actually contained within macros within the file. In order to add this custom UI to other documents we need to copy over the custom UI as well as the macros that power those commands.
To add custom UI to a set of documents we can take the following actions:
Note that the steps outlined above are just one method to accomplish this scenario. Another more practical use of this solution is to go through a list of files contained within a SharePoint library instead of a directory on disk.
The first step as outlined in the solution section above requires us to go through a directory of files. Here is the code snippet to accomplish this task:
If a file in the directory turns out to be a macro-free file we need to change the document type to be a macro-enabled file. Here is the code snippet used to accomplish this task:
Notice how the Open XML SDK provides functionality to switch document types. At this point we are ready to import our custom UI from our template into our files within the directory. Below is the code snippet necessary to grab the custom UI and vba project parts from the template document:
Now that we have the two parts from our template file we are ready to import them into our output file. Below is the code snippet necessary to accomplish this task:
Notice in the code above that I am removing any previous custom UI or vba part within the output document. Instead of deleting these parts I could have chosen to do some kind of merge, although that approach would have been more difficult.
Running this code I should end up with a directory full of macro-enabled files that all have custom UI enabled. Here is a screenshot of one of my documents, which has the imported custom UI: