使用宏从 Word 2007 中功能区的下拉列表中获取所选项目

发布于 2024-10-09 10:06:19 字数 481 浏览 0 评论 0原文

我看到以下问题,它解释了如何从下拉列表中获取所选项目:-

http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/d1cf7b3e-68cf-4b82-b806-a3431acde3b6/

以上线程建议预先有一个项目的哈希表,将选定的 id 缓存在下拉列表的 onAction() 中,并使用该选定的 id 从按钮的 onAction() 中的哈希表中查找项目。

但是,就我而言,我从数据库填充功能区 XML。该功能区 XML 显然具有用于下拉菜单的 XML,并且我正在使用宏与下拉菜单和功能区中的其他控件进行交互。我不确定如何预先建立一个可供宏使用的集合,类似于上述线程中描述的方法。

I see the following question which explains how to get the selected item from the dropdown:-

http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/d1cf7b3e-68cf-4b82-b806-a3431acde3b6/

The above thread advises to have a hashtable of the items upfront, cache the selected id in the onAction() of the dropdown and use that selected id to find the item from the hashtable in the onAction() of the button.

BUT, in my case, I populate the ribbon XML from the database. This ribbon XML obviously has the XML for dropdown and I am useing macros to interact with the dropdown and other controls in the ribbon. I am not sure how to have a collection upfront which can be used by the macro similar to approach described in the above thread.

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

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

发布评论

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

评论(2

倾听心声的旋律 2024-10-16 10:06:19

我想提出解决方案,以防万一有人遇到同样的问题:-

这就是我的功能区下拉列表的样子:-

    <dropDown id="ddlItem"  
 getItemLabel="SetTheSelectedItemInDropDown"
 onAction="GetTheSelectedItemInDropDown"  label="Items">
    <item id="Item1" label="Item1"/>
    <item id="Item1" label="Item1"/>
    <item id="Item1" label="Item1"/>
    <item id="Item1" label="Item1"/>
    <item id="Item1" label="Item1"/>
    <item id="Item1" label="Item1"/>
    <item id="Item1" label="Item1"/>
    </dropDown>

请注意 getItemLabel 和 onAction 的回调。有趣的是, getItemLabel 用于设置下拉菜单上的项目(通过下拉菜单获取)。这有点令人困惑,但这就是事实,这就是为什么我将我的方法命名为“SetTheSelectedItemInDropDown”。

onAction 的函数“GetTheSelectedItemInDropDown”用于获取所选项目。

现在以下是宏代码:-

' Declare a global variable to hold the selected item
 Dim itemName As String


' Definition of GetTheSelectedItemInDropDown which gets the selected item of the dropdown
 Sub GetTheSelectedItemInDropDown(control As IRibbonControl
 , id As String, index As Integer)
  If control.id = "ddlItems" Then
  itemName= id
  End If
 End Sub


 'Definition for SetTheSelectedItemInDropDown which sets the value in the dropdown from the global variable
 Sub SetTheSelectedItemInDropDown(control As IRibbonControl,
  index As Integer, ByRef returnedVal)
  If control.id = "ddlItems" Then
  returnedVal = itemName
  End If
 End Sub

就是这样,您现在应该能够设置和获取下拉菜单。

I wanted to put the solution just in case someone has the same problem:-

This is how my ribbon dropdown looks like :-

    <dropDown id="ddlItem"  
 getItemLabel="SetTheSelectedItemInDropDown"
 onAction="GetTheSelectedItemInDropDown"  label="Items">
    <item id="Item1" label="Item1"/>
    <item id="Item1" label="Item1"/>
    <item id="Item1" label="Item1"/>
    <item id="Item1" label="Item1"/>
    <item id="Item1" label="Item1"/>
    <item id="Item1" label="Item1"/>
    <item id="Item1" label="Item1"/>
    </dropDown>

Note the callbacks for getItemLabel and onAction. Interestingly, getItemLabel is meant for setting the item on the dropdown (get by the dropdown). It is kinda confusing but thats the way it is and thats why I named my method as "SetTheSelectedItemInDropDown".

Function "GetTheSelectedItemInDropDown" for the onAction is to get the selected item.

Now following is the macro code:-

' Declare a global variable to hold the selected item
 Dim itemName As String


' Definition of GetTheSelectedItemInDropDown which gets the selected item of the dropdown
 Sub GetTheSelectedItemInDropDown(control As IRibbonControl
 , id As String, index As Integer)
  If control.id = "ddlItems" Then
  itemName= id
  End If
 End Sub


 'Definition for SetTheSelectedItemInDropDown which sets the value in the dropdown from the global variable
 Sub SetTheSelectedItemInDropDown(control As IRibbonControl,
  index As Integer, ByRef returnedVal)
  If control.id = "ddlItems" Then
  returnedVal = itemName
  End If
 End Sub

And thats it, you should be able to set and get the dropdown now.

此刻的回忆 2024-10-16 10:06:19

下一个函数脚本适用于 Excel...
经过 2 小时的测试和谷歌搜索,我找到了一种改变和获取价值的方法。

1.)您必须有项目列表(序列),它们位于下拉列表中。

2.) 使用 .onaaction 和 application.caller 功能:(抱歉下一个脚本中的捷克语):

Sub test1()
Dim zabka As Byte
zabka = ActiveSheet.DropDowns((Application.Caller)).Value
'MsgBox zabka
Select Case zabka
Case 1
ActiveSheet.DropDowns((Application.Caller)).Text = "předání signální paré"
Case 2
ActiveSheet.DropDowns((Application.Caller)).Text = "předání čistopis"
Case 3
ActiveSheet.DropDowns((Application.Caller)).Text = "předání dokumentace SP"
End Select

End Sub

Sub aha()

With ActiveSheet.DropDowns.Add(Left:=Range("B" & 11 - 1).Left + 27, Top:=Range("B" & 11 -      1).Top, Width:=113, Height:=14)

.Caption = ""
.Name = "251"
.Text = "hoho"
.AddItem "předání signální paré"
.AddItem "předání čistopis"
.AddItem "předání dokumentace SP"
.OnAction = "test1"

'"'test1 " & xy & " '" <-way to call a sub and give a variable
End With
End Sub

Next function script is for Excel...
after 2 hour testing and google searching, i´ve found a way how to change and get value.

1.) You must have list of items (sequence), witch are in dropdowns.

2.) use .onaaction and application.caller features: (sorry for czech language in next script):

Sub test1()
Dim zabka As Byte
zabka = ActiveSheet.DropDowns((Application.Caller)).Value
'MsgBox zabka
Select Case zabka
Case 1
ActiveSheet.DropDowns((Application.Caller)).Text = "předání signální paré"
Case 2
ActiveSheet.DropDowns((Application.Caller)).Text = "předání čistopis"
Case 3
ActiveSheet.DropDowns((Application.Caller)).Text = "předání dokumentace SP"
End Select

End Sub

Sub aha()

With ActiveSheet.DropDowns.Add(Left:=Range("B" & 11 - 1).Left + 27, Top:=Range("B" & 11 -      1).Top, Width:=113, Height:=14)

.Caption = ""
.Name = "251"
.Text = "hoho"
.AddItem "předání signální paré"
.AddItem "předání čistopis"
.AddItem "předání dokumentace SP"
.OnAction = "test1"

'"'test1 " & xy & " '" <-way to call a sub and give a variable
End With
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文