自定义功能区下拉元素 - 如何查看下拉列中每个项目的ID,索引,标签

发布于 2025-02-01 14:00:19 字数 2306 浏览 5 评论 0原文

我为Excel工作簿创建了一个自定义功能区。我将自定义UI编辑器用于Microsoft Office 创建XML。我还使用自定义UI编辑器生​​成回调。我稍微修改了回调 对于我的工作簿。自定义功能区只有一个元素 - 下拉列表。

在工作表的“公司”中,我用来填充下拉的Excel数据在下面。行按公司名称按顺序排序。 第一行和最后一排是虚构的公司,只是在那里帮助我的调试。

1/1/2022  AAAAA             0
1/15/2022 ABC CO   Pencils 100
1/31/2022 DEF CO   Paper    25
2/1/2022  XYZ INC  Pens   120
1/1/2022  ZZZZZ             0

下拉菜单显示公司名称。在回调下拉dlotdown_getSelectedItemid()i指定的单元格(2,2),即“ abc co”, 因此,默认情况下,下拉列表将设置为该ID。

显然有一个问题。如果我使用下拉菜单选择“ ABC CO”,该下拉菜单已在下拉菜单中选择 不开火。我知道这是因为我在野餐事件发射时打印一条消息。但是,如果我选择下一行Def Co”, 牛肉行动会发射。

如果我可以查看下拉菜单中每个项目的索引,ID和标签,这将非常有帮助。它可能向我展示 ID为null或指示问题。但是我不知道如何查看每个项目的数据。我会非常感谢为此提供帮助。

以下是我使用的回调。如果有人看到他们的错误,我希望您能告诉我。非常感谢。

VBA Code:
Option Explicit
'testRibbon is a variable which contains the Ribbon
Public testRibbon As IRibbonUI

'Callback for customUI.onLoad
Public Sub testRibbon_onLoad(ribbon As IRibbonUI)
   Set testRibbon = ribbon
End Sub

'Callback for DropDown getItemCount
Public Sub DropDown_getItemCount(control As IRibbonControl, ByRef returnedVal)
   returnedVal = 5
End Sub

'Callback for DropDown getItemLabel
Public Sub DropDown_getItemLabel(control As IRibbonControl, index As Integer, ByRef returnedVal)
   returnedVal = Worksheets("Companies").Cells(index + 1, 2)
End Sub

'Callback for DropDown onAction
Public Sub DropDown_onAction(control As IRibbonControl, id As String, index As Integer)
   MsgBox index + " was selected"
End Sub

Public Sub DropDown_getItemID(control As IRibbonControl, index As Integer, ByRef id)
   'This Callback will set the id for each item created.
   'It provides the index value within the Callback.
   'The index is the position within the drop-down list.
   id = Worksheets("Companies").Cells(index + 1, 2)
End Sub

Public Sub DropDown_getSelectedItemID(control As IRibbonControl, ByRef id)
   'This Callback will change the drop-down to be set to a specific id.
   'This could be used to set a default value or reset the first item in the list
   id = Worksheets("Companies").Cells(2, 2)
End Sub

Public Sub updateRibbon()
'This is a standard procedure, not a Callback.  It is triggered by the button.
'It invalidates the Ribbon, which causes it to re-load.

On Error Resume Next
   testRibbon.Invalidate
On Error GoTo 0

End Sub

I have created a custom ribbon for an Excel workbook. I used the Custom UI Editor for Microsoft Office
to create the XML. I also used the Custom UI Editor to generate callbacks. I modified the callbacks slightly
for my workbook. The custom ribbon has just one element - a dropdown.

The Excel data that I used to populate the dropdown is below, in worksheet "Companies". The rows are in sorted order, by company name.
The first and last rows are fictitious companies, and just there to help my debugging.

1/1/2022  AAAAA             0
1/15/2022 ABC CO   Pencils 100
1/31/2022 DEF CO   Paper    25
2/1/2022  XYZ INC  Pens   120
1/1/2022  ZZZZZ             0

The drop down menu displays the company name. In the callback DropDown_getSelectedItemID() I specified Cells(2,2), which is "ABC CO",
so that the drop down would be set to that ID by default.

There is clearly a problem. If I use the drop down to select "ABC CO", which is already selected in the drop down, the onAction event
does not fire. I know that because I print a message when the onAction event fires. But if I select the next row, DEF CO",
onAction does fire.

It would be very helpful if I could view the index, ID and label for each of items in the drop down. It might show me that
the ID is null, or indicate the problem. But I don't know how to view that data for each item. I would be very grateful for help on that.

Below are the callbacks I use. If anyone sees an error in them I hope you will let me know. Thanks very much.

VBA Code:
Option Explicit
'testRibbon is a variable which contains the Ribbon
Public testRibbon As IRibbonUI

'Callback for customUI.onLoad
Public Sub testRibbon_onLoad(ribbon As IRibbonUI)
   Set testRibbon = ribbon
End Sub

'Callback for DropDown getItemCount
Public Sub DropDown_getItemCount(control As IRibbonControl, ByRef returnedVal)
   returnedVal = 5
End Sub

'Callback for DropDown getItemLabel
Public Sub DropDown_getItemLabel(control As IRibbonControl, index As Integer, ByRef returnedVal)
   returnedVal = Worksheets("Companies").Cells(index + 1, 2)
End Sub

'Callback for DropDown onAction
Public Sub DropDown_onAction(control As IRibbonControl, id As String, index As Integer)
   MsgBox index + " was selected"
End Sub

Public Sub DropDown_getItemID(control As IRibbonControl, index As Integer, ByRef id)
   'This Callback will set the id for each item created.
   'It provides the index value within the Callback.
   'The index is the position within the drop-down list.
   id = Worksheets("Companies").Cells(index + 1, 2)
End Sub

Public Sub DropDown_getSelectedItemID(control As IRibbonControl, ByRef id)
   'This Callback will change the drop-down to be set to a specific id.
   'This could be used to set a default value or reset the first item in the list
   id = Worksheets("Companies").Cells(2, 2)
End Sub

Public Sub updateRibbon()
'This is a standard procedure, not a Callback.  It is triggered by the button.
'It invalidates the Ribbon, which causes it to re-load.

On Error Resume Next
   testRibbon.Invalidate
On Error GoTo 0

End Sub

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

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

发布评论

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

评论(1

踏月而来 2025-02-08 14:00:19

要每次选择时,要强制牛仔锻对行为,每次选择后,下拉菜单都需要重置为空白值。在公司工作表中的值列表中添加空白值,作为第一个条目。将计数增加一(5至6)。将其添加到XML getLabel =“ dropdown_getlabel”。将其添加到您的牛仔行动呼叫中,以保存所选项目索引,然后将控件重置为默认值。将SSELECTEDITEM作为全局变量创建。

sSelectedItem = index + 1
updateRibbon

将其添加到您的回电话中,以用当前选择的项目重视下拉标签。

Public Sub DropDown_getLabel(control As IRibbonControl, ByRef label)
        on error resume next  ' to handle initial open of workbook
        label = Worksheets("Companies").Cells(sSelectedItem, 2)
End Sub

最后更新集合默认回调以选择零(空白)条目。

Public Sub DropDown_getSelectedItemIndex(control As IRibbonControl, ByRef idx)
   idx = 0  '<<<<
End Sub

To force an OnAction every time there is a selection, the Drop Down needs to be reset to a blank value after each selection. Add a blank value to the list of values in the Companies work sheet, as the first entry. Increase the count by one (5 to 6). Add this to the XML getLabel="DropDown_GetLabel". Add this to your OnAction call back to save the selected item index and then reset the control to default. Create the sSelectedItem as a global variable.

sSelectedItem = index + 1
updateRibbon

Add this to your Call Backs to value the Drop Down label with the currently selected item.

Public Sub DropDown_getLabel(control As IRibbonControl, ByRef label)
        on error resume next  ' to handle initial open of workbook
        label = Worksheets("Companies").Cells(sSelectedItem, 2)
End Sub

and finally update the set default callback to select the zero (blank) entry.

Public Sub DropDown_getSelectedItemIndex(control As IRibbonControl, ByRef idx)
   idx = 0  '<<<<
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文