VBA 检索与记录的用户名关联的用户名
我想在 VBA 中获取用户(已登录)的全名。我在网上找到的这段代码可以获取用户名:
UserName = Environ("USERNAME")
但我想要用户的真实姓名。我发现了一些有关 NetUserGetInfo 的提示,但不知道该想什么或做什么。任何提示将不胜感激 问候,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
即使这个线程相当旧,其他用户可能仍在谷歌搜索(像我一样)。
我找到了一个非常适合我的开箱即用的简短解决方案(感谢 Mr.Excel.com )。
我更改了它,因为我需要它返回带有用户全名的字符串。
原始帖子是 此处。
编辑:
好吧,我修正了一个错误,“End Sub”而不是“End Function”,并添加了一个变量声明语句,以防万一。我在Excel 2010和2013版本中进行了测试。在我的家用电脑上也运行良好(没有域,只是在工作组中)。
Even if this thread is rather old, other users might be still googling around (like me).
I found an excellent short solution that worked for me out-of-the-box (thanks to Mr.Excel.com).
I changed it because I needed it to return a string with the user's full name.
The original post is here.
EDIT:
Well, I fixed a mistake, "End Sub" instead of "End Function" and added a variable declaration statement, just in case. I tested it in Excel 2010 and 2013 versions. Worked fine on my home pc too (no domain, just in a workgroup).
我发现 API 答案也很复杂,除了需要从表单到模块重新编码之外,
下面的函数由 Rob Sampson 提供,来自 专家交流帖子。这是一个灵活的功能,详细信息请参见代码注释。请注意,这是一个 vbscript,因此变量没有标注尺寸
I found the API answer complex as well in addition to needing recoding from a form to module
The function below comes courtesy of Rob Sampson from this Experts-Exchange post. It is a flexible function, see code comments for details. Please note it was a vbscript so the variables are not dimensioned
这对我有用。它可能需要一些调整 - 我收到了几件退回的物品,但只有一件有
.Flags > 0
This works for me. It might need some adjustments - I get several items returned and only one has
.Flags > 0
尝试这个:
我建议将其重新分解到模块中,而不是将其嵌入到表单本身中。我过去曾在 Access 中成功使用过此功能。
Try this:
I would recommend re-factoring this into a module rather than embedding it in the form itself. I've used this successfully in Access in the past.
我已经尝试了很多事情,但我想我的组织不允许我查询 Active Directory(或者我的结构错误)。我只能获取我的帐户名称(不是全名)或错误“帐户名称和安全 ID 之间没有完成映射”
但是经过 2 周的搜索,我终于有了一个我想要的可行解决方案分享。我的最终提示可以在这里找到: https://www.mrexcel.com/board/threads/application-username-equivalent-in-ms-access.1143798/page-2#post-5545265
一旦我意识到这一点,使用 VBA 访问就很容易了:
我假设(但没有测试)这就是
Application.Username
来自 Excel 的使用也是如此。可能并不完美,但我终于有了一个可行的解决方案。I've tried so many things, but I suppose my organization does not allow me to query Active Directory (or I got the structure wrong). I could only get my account name (not full name) or the error "No mapping between account names and security IDs was done"
But after 2 weeks searching, I finally have a working solution that I wanted to share. My final hint can be found here: https://www.mrexcel.com/board/threads/application-username-equivalent-in-ms-access.1143798/page-2#post-5545265
Once I realized that, it was easy to access with VBA:
I assume (did not test though) that this is what
Application.Username
from Excel uses as well. Might not be perfect, but I finally have a solution that works.不就是
UserName = Application.UserName
吗文档
Isn't it just
UserName = Application.UserName
Documentation