Archive for category Office Development
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
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
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
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.
The project in which I working nowadays is a content authoring and management system and makes extensive use of Word 2007. The system has two parts in context of the problem I am going to discuss about – One part where the Admin uploads a Word 2003 (.doc) file containing all the custom styles created in there into the system. Let us call this file as a word template. The second part is where the user uploads his own content files Word 2003 (.doc) files in the system. When the user uploads the content in the system, the styles from the word template gets into the to the uploaded content file (more on this later). This facilitated the styles being introduced in the system only once (or whenever the Admin wants) using the template and the same styles getting used in all the content files without having the user to recreate them in each content file. This also offered the consistency in the styles used in the content files used in the system.
Problem :- When the user uploads the content and then later opens the content for editing, he was seeing the inbuilt styles as well and they wanted only the custom styles and “Clear Formatting” option to be seen in the content file. Silly , isn’t it. But this is was requirement.:-)
Just to make sure we we are all clear on what an in-built and what a custom style is :-
Open Word 2003 and choose Format > Styles and Formatting and what ever styles you see in the style pane are all in-built styles.
And you can create a new style by clicking on the “New Style” button. I created one “MyCustomStyle”. So this is the custom style in my file.
I need to take the attention back to my following line which I mentioned earlier :-
“When the user uploads the content in the system, the styles from the word template gets into the to the uploaded content file (more on this later). “
The way we do this is following :-
1. Convert the template word 2003 file to word 2007 format using a third party component named “Aspose.Words.dll” (www.aspose.com)
2. Convert the content word 2003 file to word 2007 using Aspose.Words.dll.
Just in case you are not aware, a Word 2007 file is an archive/zip file. You can rename any Word file (.docx) to zip file and extracts its contents as if it was a Zip file. When you look into the contents of that zip file, you will see each component of the word 2007 being represented by a file. Read about this here.
3.Copy the custom styles in the style.xml of template file to the style.xml content file.
4. Convert the content word 2007 file back to word 2003 using Aspose.Words.dll.
All the above four steps happen while the user attempts to open the file and when the file was ultimately opened, he sees the inbuilt styles as well along with the custom styles which is the problem.
When I started looking into the problem(an you, dear reader must have realized by now for sure), it seemed that user can always filter the custom styles:-
However, this is exactly what the users of the application did not want to do. So the effort of convincing them was in vain.
So, first I started looking at if Aspose.Words.dll offers any API to change the filter to show only the custom styles in the word 2003 file, when we convert the content 2007 file back to 2003. It turns out that It does not and a request for incorporating that change would take months.
I started looking at any other commercial product which would do a better job than Aspose and even the using Office Migration Utility(OFC) and wordconv.exe (comes with the Office compatibility pack). Those did not help either.
So then I looked at if we can find something in the files in the content word 2007 archive itself. There must be something in that archive which is telling Microsoft Word 2007 which types of styles to show in the style pane.
Which states that For this , the w:stylePaneFormatFilter element in the settings.xml of the docx file can have the following values:-
0x1000 – Specifies that a style should be present which removes all formatting and styles from text.
0x0002 – Specifies that only styles with the customStyle attribute should be displayed in the list of document styles.
So I sum the hex values above to get 1002 to show both Custom Styles and the Clear Formatting.
To test this, I created a new file in Word 2007 and created a new style named “MyCustomStyle” in it.
Then I rename that .docx file to .zip and then unzipped the same:-
and extracted and navigate to Word folder to see the Settings.xml:-
Opened that file and see the following structure :-
I added the following node right under the root of the xml:-
Updated the zip file and renamed the zip file back to docx and opened the docx file and saw only my custom style and “Clear All”!