设为首页收藏本站

嘻皮客娱乐学习网

 找回密码
 中文注册
搜索
打印 上一主题 下一主题
开启左侧

[Excel技巧] 用VBA获取系统用户帐户名称

[复制链接]
跳转到指定楼层
楼主
发表于 2015-4-10 10:35:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
如果要获取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
回复

使用道具 举报

小黑屋|手机版|嘻皮客网 ( 京ICP备10218169号|京公网安备11010802013797  

GMT+8, 2024-6-16 18:13 , Processed in 0.170250 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表