当前位置:首页教育技巧excel技巧excel数据库

excel表格excel利用VBA获取系统用户帐户名称

2024-07-23 20:29:08


如果要获取Windows默认的用户帐户名称,可以使用下面的VBA代码,其中使用了 WNetGetUser 函数,将代码放入标准模块中:

Option Explicit
Private Declare Function WNetGetUser Lib "mpr.dll" Alias "WNetGetUserA" _
(ByVal lpName As String, _
ByVal lpUserName As String, _
lpnLength As Long) As Long

Private Const NO_ERROR = 0
Private Const ERROR_NOT_CONNECTED = 2250&
Private Const ERROR_MORE_DATA = 234
Private Const ERROR_NO_NETWORK = 1222&
Private Const ERROR_EXTENDED_ERROR = 1208&
Private Const ERROR_NO_NET_OR_BAD_PATH = 1203&
Sub Getusername()
Dim strBuf As String, lngUser As Long, strUn As String
strBuf = Space$(255) ‘//Clear buffer
lngUser = WNetGetUser("", strBuf, 255)
If lngUser = NO_ERROR Then
strUn = Left(strBuf, InStr(strBuf, vbNullChar) – 1)
MsgBox "系统用户帐户名称是: " & strUn
Else
MsgBox "错误:" & lngUser
End If
End Sub

相关信息


电脑版

【免责声明】本站信息来自网友投稿及网络整理,内容仅供参考,如果有错误请反馈给我们及时更正,对文中内容的真实性和完整性本站不提供任何保证,不承但任何责任。
版权所有:学窍知识网 Copyright © 2011-2024 www.at317.com All Rights Reserved .