Posts Tagged Open xml sdk
Adding custom pane to the ribbon in Microsoft Word 2007 using OpenXML SDK 2.0 and VBA
Posted by Ashish Gupta in Office Development on January 12, 2011
Source code
In the last article we looked at how we add ribbon to a word document, how do we add controls to the ribbon and how do we provide interactivity to the ribbon controls and our tab looked like this:-
However, what if we have more links or more contacts (which is indeed is the case).
Looks clumsy, isn’t it? Also, it would get clumsier if we have more links and contacts. So what If we have something like this:-
Notice the little downward arrow at the right corner of each group. Clicking each would open a pane showing all the links/email addresses. Again clicking any link/email address in the custom pane would insert the link/email in the document. This way we show very few things to the user (thereby not making the ribbon clumsy) and If user wants to see more, he opens the pane to see more. This is very similar to the “Font” group in the “Home” tab.
This time we will make changes to the existing solution and following are the changes:-
a) Prepare the list of hyperlinks and email addresses to be shown to the user in the custom pane.
b) Creating the custom panes
c) Adding data to the custom panes
d) Creating that (dialog launcher) button (the button when when clicked will open the custom pane)
e) Add interactivity for the custom pane.
a) Prepare the list of hyperlinks and email addresses to be shown to the user in the custom pane :-
One way we can add the list of hyperlinks and the email addresses to the document is to add them as XML fragments (also called “CustomXmlParts”). Following is how we do it:-
i) Create XML for the list we want to show in the pane. Data for this XML can also come from the database.
ii) Add the XML to the document as CustomXmlPart
AddCustomXmlPartsToDocument():-
149-150 – Delete all the existing CustomXMLParts from the document.
152-153 – Add the TextHyperlinks XML to the document as CustomXMLPart by calling AddCustomXmlPart()
155-156 – Add the EmailLinks XML to the document as CustomXMLPart by calling AddCustomXmlPart()
iii) Add the call to AddCustomXmlPartsToDocument() to the AddRibbonToDocument()
At this point of time if we run the application, the word document does not contain anything visiblely different in the Microsoft word. However, If you zip and unzip it, you should see the custom XML parts as separate files in the zipped file.
There must be a way to give a proper name to the CustomXMLPart. I need to see that as well. That, however is not important for this example.
b) Creating the custom panes :-
We need two custom panes. One for the hyperlinks and other for the email addresses.
Open the “TheDocumentWithMacros.docm” and open the developer tab. Add two “User Forms”
In each “User Form”, add a listbox. My project explorer looks like this:-
“EmailLinksPane” user form contains a listbox with name “lstTextHyperlinks” and “TextHyperlinksPane” user form contains a listbox named “lstEmailLinks”.
Remember we created these user forms in the “TheDocumentWithMacro” and the all the user forms and the associated controls (listbox) will get copied to the document just like the macros get copied.
c) Adding data to the custom panes:-
We need to add data to the custom panes when the ribbon loads. CustomIUI XML provide a onLoad event on the ribbon element. (not showing whole of XML below)
That “RibbonLoad” is a VBA subroutine. We can get the customXMLParts from the document and put them in global variables. When the user clicks on the button, we fill the listboxes with the corresponding global variable. Following
is a dialogbox launcher. Following is the modified ribbon XML with added part highlighted:-
At this point of time if we run the application, we should see the button () for both the links and the contacts group.
e) Adding interactivity for the custom pane :-
Notice the dialogbox launcher has a button which has a action handler for onAction. In that action handler, we will load the listbox of the corresponding user form with the global variable created on ribbon onload and show the user form to the user.
At this point of time if we run the application, we should be able to show/hide the hyperlinks/email panes from the document.
User should also be able to insert the hyperlinks/email from the custom pane. For that, we need to add the code in the listbox click event which will call the same function which is called when you click a visible button which is right on the ribbon.
This is for the hyperlinks custom pane list-
This is for the emails custom pane list:-
Now If you click on any hyperlink/email item in the corresponding listbox, the hyperlink/email will be inserted in the document.