简体   繁体   English

Excel VBA-具有字符串连接的LDAP Active Directory请求

[英]Excel VBA - LDAP Active Directory request with string concatenation

I am trying to lookup contact attributes from our companies Active Directory for a tool I'm writing in Excel 2016. The program should be able to perform database lookups when the user provides either an email address or the full name ( FIRSTNAME LASTNAME ) - whatever they wish to. 我正在尝试从我们公司的Active Directory中查找我正在用Excel 2016编写的工具的联系人属性。当用户提供电子邮件地址或全名( FIRSTNAME LASTNAME )时,该程序应该能够执行数据库查找他们希望。

This works fine for email addresses and other attributes that are syntactically the same in the database as provided by the user. 对于用户在数据库中语法上相同的电子邮件地址和其他属性,此方法可以正常工作。 Unfortunately, the database does not store the full name in the format I need, which is FIRSTNAME LASTNAME 不幸的是,数据库没有以我需要的格式存储全名,即FIRSTNAME LASTNAME

So I figured i have to tell the request to also return matches for the attributes givenname and sn because those constitute the desired full name. 所以我想我必须告诉也返回匹配的请求的属性givennamesn因为这些构成了所需的全名。

In order to do that i tried the following ways of concatenating the attributes 'givenname' and 'sn'. 为了做到这一点,我尝试了以下将属性'givenname'和'sn'连接起来的方法。 Neither of them worked: 他们都没有工作:

  1. in the SQL's WHERE clause I added: 在SQL的WHERE子句中,我添加了:

     ' OR givenname & " " & sn = '" & Trim(rng.Text) & "' (see code snippet below) 
  2. In the SQL's SELECT section, I tested several ways of creating an alias: 在SQL的SELECT部分中,我测试了创建别名的几种方法:

     * ([givenname] || [sn]) As myfullname * ([givenname] + [sn]) As myfullname * ([givenname] & [sn]) As myfullname * and all of the above without square brackets and round brackets 

It seems like the SQL used in the LDAP requests is not fully supported. 似乎不完全支持LDAP请求中使用的SQL。 Because I cannot even create aliases. 因为我什至不能创建别名。 I also cannot use square brackets or functions such as CONCAT() in the SQL command. 我也不能在SQL命令中使用方括号或诸如CONCAT()函数。

Every time I tried one of the ways I got a vba error: 每次我尝试一种出现vba错误的方式时:

At least one error occurred 至少发生一个错误

(Translated from German to English) (从德语翻译成英语)

In other implementations of SQL (not in VBA) this givenname & " " & sn works perfectly fine. 在SQL的其他实现中(不在VBA中),此给定名称givenname & " " & sn可以正常工作。

Question : How can I perform a request that checks my SearchString against a concatenation of the attributes givenname (firstname) and sn (lastname) with a space between them? 问题 :如何执行一个请求,以givenname (名字)和sn (姓氏)的属性串联在一起检查我的SearchString

' Connect to active directory
Set objDSE = GetObject("LDAP://rootDSE")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open
Set objCommand = CreateObject("ADODB.Command")
Set objCommand.ActiveConnection = objConnection
SearchString = "Max Mustermann"

' Contact lookup using SQL-query
objCommand.CommandText = _
    "SELECT givenname, sn, mail, telephoneNumber, mobile, mailNickName, c, l, postalCode, department, company, streetAddress " & _
    "FROM 'LDAP://" & objDSE.Get("defaultNamingContext") & "' " & _
    "WHERE objectCategory='person' AND (mail = '" & SearchString t & "' OR givenname & sn = '" & SearchString & "')"
Set objRecordset = objCommand.Execute

If Not objRecordset.EOF Then
' Further processing which is not relevant to the question
' ...

I finally solved it in way that I was hoping I could avoid. 我终于以我希望能够避免的方式解决了它。 It is certainly not conventional nor is it efficient, but it solves the problem. 这当然不是常规的,也不是有效的,但是它解决了这个问题。 I wrote a function that would convert the SearchString into the format which the database supports. 我写了一个函数,可以将SearchString转换成数据库支持的格式。

If anyone out there has a better approach on this, feel free to post it. 如果外面有人对此有更好的处理方法,请随时发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM