excel利用VBA获取系统用户帐户名称
时间:2023-08-21 15:50:11
如果要获取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
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
删除win8.1系统登录界面电源按钮图文教程
![](https://img.aspxhome.com/file/2023/29/a253792_0s.jpg)
计算机二级考试真题-PPT-学习型社会的学习理念
Excel中VLOOKUP函数的语法和用法
如何使用天猫精灵连接蓝牙 天猫精灵连接蓝牙的方法
![](https://img.aspxhome.com/file/2023/8/a335352_0s.jpg)
Win10输入法怎么调整成Win7模式 Win10输入法调整成Win7模式方法攻略
![](https://img.aspxhome.com/file/2023/28/a245406_0s.jpg)
如何查询QQ注册过哪些网站?
![](https://img.aspxhome.com/file/2023/8/a339770_0s.jpg)
Win10怎么关闭445端口 关闭445端口的详细教程
![](https://img.aspxhome.com/file/2023/26/a225440_0s.jpg)
Win10怎么连接打印机设备?
![](https://img.aspxhome.com/file/2023/26/a222199_0s.png)
excel 2016工作区和状态栏使用教程
![](https://img.aspxhome.com/file/2023/7/a154647_0s.jpg)
wps2如何转换成pdf图解
期待吗?iOS 14 将会有这些新功能
![](https://img.aspxhome.com/file/2023/0/a195260_0s.png)
Excel2010如何制作施工进度图表
pycharm快捷键如何设置?pycharm设置快捷键的方法
![](https://img.aspxhome.com/file/2023/30/a264379_0s.png)
Excel 2007技巧:设置数值格式
![](https://img.aspxhome.com/file/2023/0/a141460_0s.jpg)
怎么把图片文字转换成word文字?
![](https://img.aspxhome.com/file/2023/6/20886_0s.jpg)
Word 2007技巧:设置图片对比度
![](https://img.aspxhome.com/file/2023/7/21667_0s.jpg)
win7系统电脑磁盘怎么清理?win7系统如何磁盘清理?
![](https://img.aspxhome.com/file/2023/30/a264176_0s.jpg)
word 如何压缩与重设图片
![](https://img.aspxhome.com/file/2023/6/33256_0s.png)
Win7旗舰版显示延迟问题的解决方法
![](https://img.aspxhome.com/file/2023/5/a316301_0s.jpg)
Win10哪个版本最好用?
![](https://img.aspxhome.com/file/2023/0/50240_0s.jpg)