4
votes

I am trying to dynamically add a new custom ribbon in a Word 2007 document following teh manual method described in this article :- http://msdn.microsoft.com/en-us/library/aa338202(v=office.12).aspx.

The article specifies the following :-

a) Create a XML file named customUI.xml which will contain the elements you want to display in the tab and put the same in a folder named customUI.

b) Rename your Word 2007 document to .zip. Add the above "customUI" folder to the zip file.

c) Add the following relationship to the "_rels/rels" file in the .zip file :-

<Relationship Type="http://schemas.microsoft.com/office/2006/
  relationships/ui/extensibility" Target="/customUI/customUI.xml" 
  Id="customUIRelID" />

Do we have some code sample to achieve the same using OpenXML SDK? For example, how to add the "RibbonExtensibilityPart" (which contains the ribbon XML) to the document?

EDIT :-

This is how I did the above mentioned steps:-

 string documentFileName = <path of the docx file>;
  string ribbonXml        =  <path of the ribbon XML file>;
 using (WordprocessingDocument myDoc = WordprocessingDocument.Open(documentFileName, true))
 {
   MainDocumentPart mainPart = myDoc.MainDocumentPart;

   if (myDoc.GetPartsCountOfType<RibbonExtensibilityPart>() > 0)
      myDoc.DeletePart(myDoc.GetPartsOfType<RibbonExtensibilityPart>().First());

   RibbonExtensibilityPart ribbonExtensibilityPart = myDoc.AddNewPart<RibbonExtensibilityPart>();
   ribbonExtensibilityPart.CustomUI = new DocumentFormat.OpenXml.Office.CustomUI.CustomUI(File.ReadAllText(ribbonXML));

   myDoc.CreateRelationshipToPart(ribbonExtensibilityPart);
 }

and I am able to see the new ribbon with the elements in it. However, I have buttons in the ribbon and I want to add handle actions on those buttons. Following is what my Ribbon XML looks like :-

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
  <ribbon>
    <tabs>
      <tab id="CustomTab" label="My Tab">
        <group id="MyGroup" label="My Group" >
          <button id="Button1" label="My Large Button" 
            size="large"/>
          <button id="Button2" label="My Normal Button" 
            size="normal" *onAction="ThisDocument.MyOtherButtonMacro"* />
        </group >
      </tab>
    </tabs>
  </ribbon>
</customUI>

have a look at the "onAction="ThisDocument.MyOtherButtonMacro". I know I can write Macro function in the document. However, as the custom ribbon will be added dynamically on the server-side, I am not sure how I can add the macro dynamically. Could anyone help?

Otaku, Thank you for your time. I checked that link earlier. He is trying to copy the ribbon from a a template to a document. I can add the ribbon to the document now (see the updated question). I am not able to add actions handlers dynamically to the document. Could you throw any idea, please.Ashish Gupta
@ydobonmai: I see what you're saying now. The only solution I've seen is to have a template ready with both the CustomUI and macros you need available, and then copy those over to your document that you're editing and change the extension to a .docm. Take a look at this page which outlines these steps using the SDK: official2007.com/blog/office-2007/the-open-xml-sdk2Todd Main
@Otaku, I see that when you add a macro to a word 2007 file, you have a relationship created with namespace "schemas.microsoft.com/office/2006/relationships/vbaProject" and a "vbaproject.bin" file created inside the compressed zip file. There must be a way. I was looking into and saw something like :- VbaProjectPart vbaProjectPart = myDoc.AddNewPart<VbaProjectPart>(); VbaDataPart vbaDataPart = vbaProjectPart.AddNewPart<VbaDataPart>(); However, I am still trying to figure out how to put "code" for that VbaProject. Please let me know If you get to see anything.Ashish Gupta
@ydobonmai: all it is is pre-written VBA code that works with your Ribbon and any/all of the onAction calls you need. so in your case you would already have code for MyOtherButtonMacro in that template's VBA project (and any other routines needed). Then, once you copy it over, it will work with your ribbon. Once you have all your code in your templates VBA project, it will be the "vbaproject.bin". Dirctly editing that from the SDK, AFAIK, is impossible as it's a completely different format.Todd Main