人气 5300

EXCEL VBA 取当前登录 用户名的多种实现方法 [复制链接]

精益 2017-4-5 15:30:12
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
您需要登录后才可以回帖 登录 | 立即注册

QQ|手机版|精益人 ( 沪ICP备19004111号-1 )|网站地图

GMT+8, 2024-12-22 18:44 , Processed in 0.266340 second(s), 20 queries .

Powered by Lean.ren X3.5 Licensed  © 2001-2030 LEAN.REN