我有一些用于获取用户经理的代码 - 我最初为 SharePoint 网站编写了代码,但最近被要求使用 VBA 编写它。
Const SearchField = "DisplayName"
Const ReturnField = "manager"
Public Function MLookup(ByVal SearchString As String) As String
Application.ScreenUpdating = False
Dim strDomain
strDomain = GetObject("LDAP://rootDSE").Get("defaultNamingContext")
Dim objConnection As ADODB.Connection
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"
Dim objCommand As ADODB.Command
Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.CommandText = _
"<LDAP://" & strDomain & ">;(&(objectCategory=User)" & _
"(" & SearchField & "=" & SearchString & "));" & SearchField & "," & ReturnField & ";subtree"
Dim objRecordSet As ADODB.Recordset
Set objRecordSet = objCommand.Execute
If objRecordSet.RecordCount = 0 Then
MLookup = "Not Found"
Else
objMngr = objRecordSet.Fields(ReturnField)
objMngr = Mid(objMngr, 4, InStr(1, objMngr, ",OU"))
objMngr = Replace(objMngr, "\,", ",")
objMngr = Left(objMngr, Len(objMngr) - 12)
MLookup = Trim(objMngr)
End If
objConnection.Close
Set objRecordSet = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
Application.ScreenUpdating = True
End Function
这段代码运行良好 - 运行有点慢,因为我在家工作,但它完成了工作。不过,我现在遇到了障碍。manager 字段返回 manager 上的用户字符串,但刺痛是使用 Distinguished Name 构建的 - 对于女士来说,这意味着她们的娘家姓。该数据在报告中使用并且激怒了一些羽毛,因为数据在他们看来是不正确的。我想知道是否有办法将经理的返回字符串修改为该人的显示名称,而不是从经理字段中解析名称?或者是我创建第二个返回显示名称的函数的唯一解决方案(这会使我的响应时间加倍并且可能太多的 5000 条记录)?