如何在Excel中获取登录用户的电子邮件地址

问题描述 投票:0回答:3

我尝试通过当前登录用户的 Excel VBA 获取邮件地址(不使用 Outlook)。

我可以得到

Application.UserName

但这还不够。

Excel -> 文件 -> 帐户下的“属于:[电子邮件受保护]”下有邮件地址

有办法用VBA提取它吗?

excel vba
3个回答
4
投票

我找到了优雅的解决方案。可能不能保证 Excel 用户,但非常适合我。

Sub getUserMail()
    Debug.Print CreateObject("WScript.Shell").RegRead("HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Common\Identity\ADUserName")
End Sub

感谢 @Storax 的注册表项以及如何轻松读取注册表的答案。 在 VBA 中读写注册表


1
投票

恐怕没有 Outlook,这是不可能的...如果讨论中的用户安装并配置了 Outlook,您可以使用下一个代码获取它。该代码需要引用“Microsoft Outlook ...对象库”:

Sub ActiveUserMailAddress()
  Dim objOutlook As New Outlook.Application
  Debug.Print objOutlook.GetNamespace("MAPI").CurrentUser.Address
End Sub

要添加参考:在

VBE
中,转到工具 -> 参考... -> 向下滚动直到看到上面推荐的参考,选中它并按
OK


0
投票

在我的机器上,我在注册表中找不到“ADUserName”键。但下面的代码可以工作:

Sub Button1_Click()
    Set WShell = CreateObject("WScript.Shell")
    ConnectedAccountWamAad = WShell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Common\Identity\ConnectedAccountWamAad")
    Email = WShell.RegRead("HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Common\Identity\Identities\" & ConnectedAccountWamAad & "_ADAL\EmailAddress")
    
    MsgBox "Email: " & Email
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.