如何在 Word 中使用 VBA 从下拉列表(内容控件)中获取选定值 [英] How get selected value from drop-down list (Content control) using VBA in Word

查看:123
本文介绍了如何在 Word 中使用 VBA 从下拉列表(内容控件)中获取选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是用 word 做 VBA 的新手,但在 excel 中使用了很多.我的 Word 文档中有一个内容控制组合框.我有可供选择的员工姓名,以及我为他们的电子邮件地址设置的值.这个想法是通过选择一个复选框,向从组合框中选择的人发送一个电子邮件地址.我现在如何在选择复选框时编写代码来发送电子邮件,但我正在努力从组合框中获取值(即电子邮件地址).我想要的只是一个代码,它为我提供了从名为审阅者"的组合框中选择的值.我需要有关所有代码的帮助,包括如何进行 DIM.

I am new doing VBA in word, but use quite a lot in excel. I have a Content Control Combo box in my word document. I have names of employees to choose from, and the value I have set to their email address. The idea is to send an email address to the person chosen from the combo box, by selecting a check box. I now how to do code to send the email when selecting the checkbox, but I am struggling with getting the Value (ie email address) from the combo box. All I want is a code that gives me the selected value from my combo box named "Reviewers". I need help with all the code, including how to DIM.

推荐答案

例如,作为直接从审阅者"下拉列表运行的 ContentControlOnExit 宏:

For example, as a ContentControlOnExit macro that runs directly from your 'Reviewers' dropdown:

Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim i As Long, StrEmail As String
With ContentControl
  If .Title = "Reviewers" Then
  For i = 1 To .DropdownListEntries.Count
    If .DropdownListEntries(i).Text = .Range.Text Then
      StrEmail = .DropdownListEntries(i).Value
      MsgBox StrEmail 
      Exit For
    End If
  Next
  End If
End With
End Sub

或者,作为直接从名为发送电子邮件"的复选框运行的 ContentControlOnExit 宏:

Alternatively, as a ContentControlOnExit macro that runs directly from a Checkbox titled 'Send Email':

Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
Dim i As Long, StrEmail As String
With CCtrl
  If .Title = "Send Email" Then
    If .Checked = True Then
      With .Parent.SelectContentControlsByTitle("Reviewers")(1)
        For i = 1 To .DropdownListEntries.Count
          If .DropdownListEntries(i).Text = .Range.Text Then
            StrEmail = .DropdownListEntries(i).Value
            MsgBox StrEmail
            Exit For
          End If
        Next
      End With
    End If
  End If
End With
End Sub

这篇关于如何在 Word 中使用 VBA 从下拉列表(内容控件)中获取选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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