如何获取 Microsoft Word 中下拉菜单的值

发布于 2024-07-25 12:11:03 字数 79 浏览 1 评论 0原文

我有一个 Microsft Word 文档,其中有一个下拉菜单。 我正在尝试编写一个宏来获取下拉列表的值并执行计算。 如何获取下拉菜单的值?

I have a Microsft Word document with a drop-down menu in it. I am trying to write a macro that gets the value of the drop down and performs calculations. How do I get the value of the drop-down?

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

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

发布评论

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

评论(1

苦行僧 2024-08-01 12:11:03

首先,您可以通过您指定的名称(“属性”框中的“书签”字段)或您添加的字段序列中的编号来获取对下拉列表的引用。

然后使用 Result 属性显示当前选定的值。

我创建了一个空白文档,上面有两个下拉列表(显示“表单”工具栏,然后单击一个按钮以在光标位置插入该控件):

ColorDropdown
  red
  green
  blue

SizeDropdown
  small
  medium
  large

然后我编写了一些测试例程:

Sub ListDropDowns()
  Dim doc As Document, f As FormField
  Set doc = ActiveDocument

  For Each f In doc.FormFields
    Say f.Name & " = " & f.Result
  Next
End Sub

Sub ShowChosenColor()
  Dim f As FormField
  Set f = ActiveDocument.FormFields("ColorDropdown")
  Say "color = " & f.Result
End Sub

Sub Say(s As String)
  Debug.Print s
End Sub

这些是来自“立即”的 ListDropDowns 和 ShowChosenColor 的结果窗口:

ColorDropdown = blue
SizeDropdown = large

color = blue

更多立即窗口测试:

set doc = ActiveDocument

? doc.FormFields(1).Name
ColorDropdown

? doc.FormFields(2).Name
SizeDropdown

? doc.FormFields(2).Result
large


sName = doc.FormFields(2).Name
? sName
SizeDropdown

sSize = doc.FormFields(sName).Result
? sSize
large

First, you can get a reference to the dropdown either by the name you gave it (the Bookmark field in the Properties box), or by its number in the sequence of fields you added.

Then you use the Result property to show the currently selected value.

I created a blank document with two dropdown lists on it (show the Forms toolbar, then click a button to insert that control at the cursor position):

ColorDropdown
  red
  green
  blue

SizeDropdown
  small
  medium
  large

then I wrote a few test routines:

Sub ListDropDowns()
  Dim doc As Document, f As FormField
  Set doc = ActiveDocument

  For Each f In doc.FormFields
    Say f.Name & " = " & f.Result
  Next
End Sub

Sub ShowChosenColor()
  Dim f As FormField
  Set f = ActiveDocument.FormFields("ColorDropdown")
  Say "color = " & f.Result
End Sub

Sub Say(s As String)
  Debug.Print s
End Sub

these are the results of ListDropDowns and ShowChosenColor from the Immediate window:

ColorDropdown = blue
SizeDropdown = large

color = blue

some more Immediate window test:

set doc = ActiveDocument

? doc.FormFields(1).Name
ColorDropdown

? doc.FormFields(2).Name
SizeDropdown

? doc.FormFields(2).Result
large


sName = doc.FormFields(2).Name
? sName
SizeDropdown

sSize = doc.FormFields(sName).Result
? sSize
large
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文