使用宏从Word 2007中的功能区中的下拉列表中获取选定的项目 [英] Getting the selected item from the dropdown in a ribbon in Word 2007 using macro

查看:105
本文介绍了使用宏从Word 2007中的功能区中的下拉列表中获取选定的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

上面的线程建议预先准备一个项目的哈希表,将选定的ID缓存在下拉列表的onAction()中,并使用该选定的ID从按钮的onAction()中的哈希表中找到该项目.

但是,以我为例,我从数据库中填充了功能区XML.该功能区XML显然具有用于下拉菜单的XML,我正在使用宏与功能区中的下拉菜单和其他控件进行交互.我不确定如何预先设置一个可被宏使用的集合,类似于上述线程中所述的方法.

解决方案

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

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

    <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 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.

解决方案

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.

这篇关于使用宏从Word 2007中的功能区中的下拉列表中获取选定的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆