EXCEL VBA 取当前登录 用户名的多种实现方法1. 使用Wscript Dim wsh As Object Set wsh = CreateObject("WScript.Network") Sheet1.Range("a1") = wsh.UserName2. 使用环境变量
dim iuseriuser = Environ("username")
3.VBA获取excel文件当前用户名的代码 (与windows用户名看看有否区别)Public Sub 获取当前工作表的用户名()
MsgBox "当前用户名是: " & Application.UserNameEnd Sub
4. 使用API 方法获取 用户名‘API调用
Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long
’转成VBA里常规的字符
Public Function NTDomainUserName() As String
Dim strBuffer As String * 255
Dim lngBufferLength As Long
Dim lngRet As Long
Dim strtemp As String
lngBufferLength = 255
strBuffer = String(255, 0)
lngRet = GetUserName(strBuffer, lngBufferLength)
strtemp = LCase(Left(strBuffer, lngBufferLength - 1))
NTDomainUserName = strtemp
End Function