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

Ash*_*pta 1 ribbon ms-word word-vba

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

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

上面的线程建议事先获得项目的哈希表,在下拉列表的onAction()中缓存选定的id,并使用该选定的id从按钮的onAction()中的哈希表中查找项目.

但是,就我而言,我从数据库中填充了功能区XML.这个功能区XML显然有用于下拉列表的XML,我使用宏来与功能区中的下拉列表和其他控件进行交互.我不确定如何预先设置一个可以由宏使用的集合,类似于上述线程中描述的方法.

Ash*_*pta 6

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

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

    <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>
Run Code Online (Sandbox Code Playgroud)

注意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
Run Code Online (Sandbox Code Playgroud)

就是这样,你应该能够设置并获得下拉列表.