如何从 VBA 获取当前登录的 Active Directory 用户名?

2023-12-10

我是 Active Directory 新手。

我有一个 VBA Excel 加载项,当且仅当运行该加载项的计算机当前登录到 Active Directory(无论是本地登录还是通过 VPN)时,该加载项才应运行。

知道了域名,我如何检索当前登录用户的用户名?

Thanks!


我知道现在有点晚了,但去年我费了九牛二虎之力才找到下面的代码,它可以返回用户名(“fGetUserName()”)或全名(“DragUserName()”)。您甚至不需要知道 ad/dc 地址。

希望这对咨询这个问题的人有所帮助。

Private Type USER_INFO_2
    usri2_name As Long
    usri2_password  As Long  ' Null, only settable
    usri2_password_age  As Long
    usri2_priv  As Long
    usri2_home_dir  As Long
    usri2_comment  As Long
    usri2_flags  As Long
    usri2_script_path  As Long
    usri2_auth_flags  As Long
    usri2_full_name As Long
    usri2_usr_comment  As Long
    usri2_parms  As Long
    usri2_workstations  As Long
    usri2_last_logon  As Long
    usri2_last_logoff  As Long
    usri2_acct_expires  As Long
    usri2_max_storage  As Long
    usri2_units_per_week  As Long
    usri2_logon_hours  As Long
    usri2_bad_pw_count  As Long
    usri2_num_logons  As Long
    usri2_logon_server  As Long
    usri2_country_code  As Long
    usri2_code_page  As Long
End Type

Private Declare Function apiNetGetDCName Lib "Netapi32.dll" Alias "NetGetDCName" (ByVal servername As Long, ByVal DomainName As Long, bufptr As Long) As Long

Private Declare Function apiNetAPIBufferFree Lib "Netapi32.dll" Alias "NetApiBufferFree" (ByVal buffer As Long) As Long

Private Declare Function apilstrlenW Lib "kernel32" Alias "lstrlenW" (ByVal lpString As Long) As Long

Private Declare Function apiNetUserGetInfo Lib "Netapi32.dll" Alias "NetUserGetInfo" (servername As Any, UserName As Any, ByVal level As Long, bufptr As Long) As Long

Private Declare Sub sapiCopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

Private Declare Function apiGetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Private Declare Function GetComputerName Lib "kernel32.dll" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Private strUserID As String

Private strUserName As String

Private strComputerName As String

Private Const MAXCOMMENTSZ = 256
Private Const NERR_SUCCESS = 0
Private Const ERROR_MORE_DATA = 234&
Private Const MAX_CHUNK = 25
Private Const ERROR_SUCCESS = 0&

Public Function fGetUserName() As String
 ' Returns the network login name
Dim lngLen As Long, lngRet As Long
Dim strUserName As String
    strUserName = String$(254, 0)
    lngLen = 255
    lngRet = apiGetUserName(strUserName, lngLen)
        If lngRet Then
            fGetUserName = Left$(strUserName, lngLen - 1)
        End If
End Function

Private Sub Class_Initialize()
On Error Resume Next
'Returns the network login name
Dim strTempUserID As String, strTempComputerName As String

'Create a buffer
strTempUserID = String(100, Chr$(0))
strTempComputerName = String(100, Chr$(0))

'Get user name
GetUserName strTempUserID, 100

'Get computer name
GetComputerName strTempComputerName, 100

'Strip the rest of the buffer
strTempUserID = Left$(strTempUserID, InStr(strTempUserID, Chr$(0)) - 1)
Let strUserID = LCase(strTempUserID)

strTempComputerName = Left$(strTempComputerName, InStr(strTempComputerName, Chr$(0)) - 1)
Let strComputerName = LCase(strTempComputerName)

Let strUserName = DragUserName(strUserID)

End Sub

Public Property Get UserID() As String
    UserID = strUserID
End Property

Public Property Get UserName() As String
    UserName = strUserName
End Property

Public Function DragUserName(Optional strUserName As String) As String
On Error GoTo ErrHandler
Dim pBuf As Long
Dim dwRec As Long
Dim pTmp As USER_INFO_2
Dim abytPDCName() As Byte
Dim abytUserName() As Byte
Dim lngRet As Long
Dim i As Long

    ' Unicode
    abytPDCName = fGetDCName() & vbNullChar
    If strUserName = "" Then strUserName = fGetUserName()
    abytUserName = strUserName & vbNullChar

    ' Level 2
    lngRet = apiNetUserGetInfo( _
                            abytPDCName(0), _
                            abytUserName(0), _
                            2, _
                            pBuf)
    If (lngRet = ERROR_SUCCESS) Then
        Call sapiCopyMem(pTmp, ByVal pBuf, Len(pTmp))
        DragUserName = fStrFromPtrW(pTmp.usri2_full_name)
    End If

    Call apiNetAPIBufferFree(pBuf)
ExitHere:
    Exit Function
ErrHandler:
    DragUserName = vbNullString
    Resume ExitHere
End Function

Public Property Get ComputerName() As String
    ComputerName = strComputerName
End Property

Private Sub Class_Terminate()
    strUserName = ""
    strComputerName = ""
End Sub

Public Function fGetDCName() As String
Dim pTmp As Long
Dim lngRet As Long
Dim abytBuf() As Byte

    lngRet = apiNetGetDCName(0, 0, pTmp)
    If lngRet = NERR_SUCCESS Then
        fGetDCName = fStrFromPtrW(pTmp)
    End If
    Call apiNetAPIBufferFree(pTmp)
End Function

Public Function fStrFromPtrW(pBuf As Long) As String
Dim lngLen As Long
Dim abytBuf() As Byte

    ' Get the length of the string at the memory location
    lngLen = apilstrlenW(pBuf) * 2
    ' if it's not a ZLS
    If lngLen Then
        ReDim abytBuf(lngLen)
        ' then copy the memory contents
        ' into a temp buffer
        Call sapiCopyMem( _
                abytBuf(0), _
                ByVal pBuf, _
                lngLen)
        ' return the buffer
        fStrFromPtrW = abytBuf
    End If
End Function
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从 VBA 获取当前登录的 Active Directory 用户名? 的相关文章

  • Windows 身份验证在 IISExpress 中有效,但在 IIS 中无效

    我有一个奇怪的问题 我正在 Visual Studio 2013 中使用最新的 MVC5 我正在尝试创建一个托管匿名 API 的站点以及一个需要通过 Windows 身份验证的 Intranet 域凭据的管理仪表板 当通过 IIS Expr
  • 通过文本自动创建到另一个工作表的超链接

    我想知道如何基于各自工作表中两个单元格具有的相同文本值 通过脚本自动创建从一个 Excel 工作表到另一个 Excel 工作表的超链接 如果这可以在没有脚本的情况下完成 使用某种公式 如 VLOOKUP 这将是更好的选择 谢谢你的时间 使用
  • Excel Q - 带有二维数组的 SUMIFS

    我有一个二维数组 水平轴上的日期和垂直轴上的标识号 我想要以特定日期和 ID 为条件的总和 并且我想知道如何使用 SUMIFS 来执行此操作 由于某种原因 我似乎不能 因为数组是二维的 而标准范围是一维的 谁能给我关于我可以使用的其他公式的
  • Excel - 根据选择创建图表的宏

    我想就以下问题寻求您的帮助 因为我必须创建大量图表 所以我想要一个宏来根据我的选择插入图表 由于我对 VBA 没有任何了解 但现在需要它 至少现在 我真的应该自己学习使用它 我将感谢您的帮助 基本上 我需要知道如何调整我记录的代码 以便根据
  • 如何粘贴到Excel B列的最后一行?

    我需要将单元格从 H2 L2 一直向下剪切并将其粘贴到 B 列的最后一行 数据每次都会不同 所以我无法对任何范围进行硬编码 VBA 代码会很好 从 H2 L2 向下剪切并粘贴 插入到 B 列的最后一行 到目前为止我得到了 Range H2
  • 如何在 MS Word 中的每个标题末尾应用宏?

    我有一个包含各种标题的文档 因此不一定是标题 1 或标题 2 而是所有类型的标题 我想做的是编写一个宏 例如 删除每个标题末尾的 2 个空格 例如 我们有 这是一个标题 在标题的最后 我会这样做 Selection Delete Unit
  • 拆分具有多行文本和单行文本的行

    我试图弄清楚如何拆分数据行 其中行中的 B C D 列包含多行 而其他列不包含多行 我已经弄清楚如何拆分多行单元格 如果我将这些列复制到新工作表中 手动插入行 然后运行下面的宏 仅适用于 A 列 但我在编码时迷失了休息 Here s wha
  • IE.navigate2 因保护模式关闭而失败

    我正在从 Excel VBA 自动化 IE8 Excel 2010 Windows 7 Set IE CreateObject InternetExplorer Application IE Navigate2 URL 如果 URL 是处于
  • 需要在Excel中合并3列

    我有 3 列 A B C 我需要合并这 3 列 并且我已经应用了 forumala A1 B1 C1输出为 E 列 我需要输出为 D 列 下面的公式将达到您想要的结果 TEXTJOIN TRUE A1 C1 Textjoin 的工作方式类似
  • Excel VBA - 如何逐行读取csv文件而不是整个文件

    这是我需要读取的 csv 文件内容 header header header header header header value value value value value value value value value 我在网上找到
  • MS Access 中的舍入

    VBA Access 中舍入的最佳方法是什么 我目前的方法是利用Excel方法 Excel WorksheetFunction Round 但我正在寻找一种不依赖Excel的方法 请注意 VBA Round 函数使用 Banker 舍入 将
  • 我需要代码在两行之间复制并粘贴到另一张表中,并给出任何值?

    例如 我有 50 行数据 第一行有学生的名字 我需要代码将数据从 RAM 复制到 RAMESH 在这之间我有 20 行 我需要代码来复制行并将其粘贴到另一张纸中 它不应该问我名字 默认情况下 它必须采用 RAM 和 RAMESH 名称 好的
  • Excel VBA 导出到文本文件。需要删除空行

    我有一个工作簿 使用以下脚本将其导出到文本文件 它工作正常 但是当我打开文本文件时 末尾总是有一个空行 这导致我在生成此文本文件后运行的另一个脚本出现问题 有关如何从导出中删除空行的任何帮助 Code Sub Rectangle1 Clic
  • 如何在不滚动的情况下截取整个电子邮件正文?

    我正在使用 OL2010 想要制作整个电子邮件的屏幕截图 不仅仅是 屏幕 可以用VBA或者外部程序来完成吗 有一个类似的问题 https stackoverflow com questions 4176340关于如何使用 C 实现这一点 注
  • OpenArgs 为空问题

    我正在使用OpenArgs使用时发送值的参数DoCmd OpenForm DoCmd OpenForm frmSetOther acNormal acFormAdd acDialog value 然后我用Me OpenArgs在打开的表格内
  • 证明 Excel VBA Scripting.Dictionary 不保留项目插入顺序

    我正在尝试决定是否为我的项目使用 Excel VBA 集合或字典 出于多种原因 我倾向于字典 但在使用字典时我会继续阅读它For Each循环检索字典项目或从字典 Items 数组读取项目时 检索顺序可能不是添加项目的顺序 这对于我的应用程
  • 复制一张工作表上的静态范围,然后根据单元格中的单个值粘贴到另一张工作表中的动态范围

    我对这个问题分为三个部分 我在 Sheet1 A1 中有一个带有周数的单元格 我在 Sheet1 B1 F1 中有一个需要复制的静态范围 然后 我需要将该值粘贴到 Sheet2 中的动态范围中 偏移量为行的周数 这是我正在为我经常使用的工作
  • 根据单元格值向用户窗体添加复选框

    我对 VBA 很陌生 只有 3 天 但我发现它非常有用且易于使用 但现在我面临一个问题 我需要制作一个具有不同复选框的用户窗体 但我需要根据工作表某一列中使用的信息自动添加它们 我相信我可以使用 For Each Next 但我真的不知道如
  • VBA 字符串 255 个字符限制

    我在使用 VBA 时遇到问题 并注意到它的字符串限制为 255 个字符 我实际上正在尝试通过 POST 发送 JSON 并暂停执行 我注意到该字符串始终只有 255 个字符 有没有办法调整字符串的大小或其他什么 我在这个问题上浪费了大约 6
  • 无法摆脱脚本中的硬编码延迟

    我用 vba 结合 selenium 编写了一个脚本来解析网页中可用的所有公司名称 该网页启用了延迟加载方法 因此每个滚动中只有 20 个链接可见 如果我滚动 2 次 则可见链接数为 40 个 依此类推 该网页中有 1000 个可用链接 我

随机推荐