在 Word 中使用 VBA 从 Active Directory 获取电子邮件地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28328036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 06:06:24  来源:igfitidea点击:

Get email address from Active Directory using VBA in Word

vbaemailoutlookword-vba

提问by jagdfuerst

I have Word VBA code which sends a document via mail. The recipient email address is manually written into a textbox and then committed automatically to Outlook.

我有通过邮件发送文档的 Word VBA 代码。收件人电子邮件地址手动写入文本框,然后自动提交到 Outlook。

I want that the email address is automatically set into that textbox by using the name of a person (also textboxes for firstname + lastname), comparing that to the Active Directory and retrieving the appropriate email address.

我希望通过使用人名(也是名字 + 姓氏的文本框)将电子邮件地址自动设置到该文本框中,将其与 Active Directory 进行比较并检索适当的电子邮件地址。

How to do that in Word VBA?

如何在 Word VBA 中做到这一点?

回答by Tim Williams

Try this out: it will query back an email based on either a username (name1 - do not pass a second parameter) or first (name1) and last (name2) names.

试试这个:它会根据用户名(name1 - 不要传递第二个参数)或名字(name1)和姓氏(name2)查询回电子邮件。

Function UserNameToEmail(name1 As String, Optional name2 As String = "")

    Set rootDSE = GetObject("LDAP://RootDSE")
    base = "<LDAP://" & rootDSE.Get("defaultNamingContext") & ">"

    'filter on user objects with the given account name
    fltr = "(&(objectClass=user)(objectCategory=Person)"
    If Len(name2) = 0 Then
        fltr = fltr & "(sAMAccountName=" & name1 & "))"
    Else
        fltr = fltr & "(givenName=" & name1 & ")(sn=" & name2 & "))"
    End If

    'add other attributes according to your requirements
    attr = "mail,department,givenName,sn"
    scope = "subtree"

    Set conn = CreateObject("ADODB.Connection")
    conn.Provider = "ADsDSOObject"
    conn.Open "Active Directory Provider"

    Set cmd = CreateObject("ADODB.Command")
    Set cmd.ActiveConnection = conn
    cmd.CommandText = base & ";" & fltr & ";" & attr & ";" & scope

    Set rs = cmd.Execute

    If Not rs.EOF Then
      For Each f In rs.Fields
        Debug.Print f.Name & ": " & f.Value
      Next f
      UserNameToEmail = rs.Fields("mail").Value
    End If

    rs.Close
    conn.Close

End Function

FYI- list of ActiveDirectory fields: http://www.kouti.com/tables/userattributes.htm

仅供参考 - ActiveDirectory 字段列表:http: //www.kouti.com/tables/userattributes.htm

回答by Dmitry Streblechenko

Are you using the Outlook Object Model? Callin Recipient.Resolve or Recipeints.ResolveAll will resolve the display names of the message recipients.

您在使用 Outlook 对象模型吗?Callin Recipient.Resolve 或 Recipeints.ResolveAll 将解析消息收件人的显示名称。

If you want to resolve a name to an address without creating a message, use Namespace.CreateRecipient / Recipeint.Resolve.

如果您想在不创建消息的情况下将名称解析为地址,请使用 Namespace.CreateRecipient / Recipeint.Resolve。