开发者

Open XML SDK - Adding a macro programatically to a Word 2007 document

开发者 https://www.devze.com 2023-01-31 09:44 出处:网络
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.

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.CreateRelationsh开发者_如何学CipToPart(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?

0

精彩评论

暂无评论...
验证码 换一张
取 消