Pergunta

I want to limit the number of buttons in the group of a ribbon in Word 2007 as the buttons are getting generated on the basis of data in the database. See the pic below. Too many buttons showed. I wanted to have limited number, say 6 or so in the ribbon with a dialogboxlauncher which when clicked will open a pane showing all the buttons. Is there any way of doing the same. Can somebody also tell me how to create that pane when somebody clicks on the dialogbox launcher?

Foi útil?

Solução

The way I did it is to load some (say 6) of the items in the ribbon as buttons and add all the items as the CustomXMlPart in the document. In the document, I added a user control which contained a listbox. On the ribbon load, I get all the items from the CustomXmlPart and put them in the listbox. On the dialogbox launcher button click, I show/hide the user control to show all the items in the list.

Following are the steps in detail :-

a) Get all the items from the database and keep that in collection. b) Generate the ribbon XML like the following withe the 6 buttons from the above collection :-

<?xml version="1.0" encoding="utf-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="RibbonLoad">
  <ribbon>
    <tabs>
      <tab id="tabMyTab" label="MyTab">
        <group id="grpItems" label="My items">
          <button id="test1" label="test1"/>
          <button id="test2" label="test2"/>
          <button id="test3" label="test3"/>
          <button id="test4" label="test4"/>
          <button id="test5" label="test5"/>
          <button id="test6" label="test6"/>
          <dialogBoxLauncher>
            <button id="btnShowAllItems" label="Show all custom tags" onAction="ShowAllItems" />
          </dialogBoxLauncher>
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

c) Add the collection as the CustomXMLPart to the document :-

static void AddCustomTableXmlPart(WordprocessingDocument document)
{
            MainDocumentPart mainDocumentPart = document.MainDocumentPart;
            XDocument customTagsXml = GetAllItemsAsCustomXML();

            if (mainDocumentPart.GetPartsCountOfType<CustomXmlPart>() > 0)
                mainDocumentPart.DeleteParts<CustomXmlPart>(mainDocumentPart.CustomXmlParts);

            //Add a new customXML part and then add content
            var customXmlPart = mainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);

            //copy the XML into the new part...
            using (var ts = new StreamWriter(customXmlPart.GetStream()))
            {
                ts.Write(customTagsXml.ToString());
                ts.Flush();
            }
 }

d) Go to the developer tab of your docm file, add a UserForm to the project and add a listbox to it. Write a subroutine which would load the items from the CustomXMlPart added already to the document and add those items to the listbox in the userform. Something like below :-

Sub LoadItems()
     Dim totalItemsCount As Integer
     totalItemsCount = ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).SelectNodes("//Items")(1).ChildNodes.Count
     Dim item As String

     For i = 1 To totalItemsCount
        item = ActiveDocument.CustomXMLParts(ActiveDocument.CustomXMLParts.Count).SelectNodes("//Items")(1).ChildNodes(i).text
        ' I had to remove the spaced before adding it as It was throwing errors
        item = Replace(item, " ", Empty)

        If Len(item) > 1 Then
        ItemUserControl.lstItems.AddItem pvargItem:item
        End If
     Next i
End Sub

e) Define the sub named RibbonLoad which is called from the onLoad event of the ribbon (check the RibbonXML). Call the LoadItems sub from this RibbonLoad sub.

Sub RibbonLoad(ribbon As IRibbonUI)
LoadItems
End Sub

f) Define the following sub which will show/hide the user control. This is called on the onAction of the dialogBoxLauncher button. (see the RibbonXML)

Sub ShowAllItemss(control As IRibbonControl)
    If ItemsUserControl.Visible = False Then
         If ItemsUserControl.lstItems.ListCount = 0 Then
            LoadCustomTags
         End If
         ItemsUserControl.Show
    Else
         ItemsUserControl.Hide
    End If
End Sub
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top