OpenXML - 限制自定义 MS-Word 功能区中组中显示的按钮数量

发布于 2024-10-09 01:53:10 字数 213 浏览 10 评论 0 原文

我想限制 Word 2007 中功能区组中的按钮数量,因为按钮是根据数据库中的数据生成的。请参阅下图。 显示的按钮太多。我希望功能区中的数量有限,例如 6 个左右,并带有一个对话框启动器,单击该启动器将打开一个显示所有按钮的窗格。有没有办法做同样的事情。有人还可以告诉我当有人单击对话框启动器时如何创建该窗格吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

大姐,你呐 2024-10-16 01:53:10

我的方法是将功能区中的一些(例如 6 个)项目作为按钮加载,并将所有项目添加为文档中的 CustomXMlPart。在文档中,我添加了一个包含列表框的用户控件。在功能区加载中,我从 CustomXmlPart 获取所有项目并将它们放入列表框中。在对话框启动器按钮上单击,我显示/隐藏用户控件以显示列表中的所有项目。

以下是详细步骤:-

a) 从数据库中获取所有项目并将其保留在集合中。
b) 使用上述集合中的 6 个按钮生成如下所示的功能区 XML:-

<?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) 将集合作为 CustomXMLPart 添加到文档中:-

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) 转到 docm 文件的开发人员选项卡,将 UserForm 添加到项目中并向其添加一个列表框。编写一个子例程,该子例程将加载已添加到文档中的 CustomXMlPart 中的项目,并将这些项目添加到用户窗体中的列表框中。如下所示:-

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) 定义名为 RibbonLoad 的子项,该子项从功能区的 onLoad 事件中调用(检查 RibbonXML)。从此 RibbonLoad 子程序调用 LoadItems 子程序。

Sub RibbonLoad(ribbon As IRibbonUI)
LoadItems
End Sub

f) 定义以下子控件,它将显示/隐藏用户控件。这是在dialogBoxLauncher 按钮的onAction 上调用的。 (请参阅 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文