如何获取当前登录用户的电子邮件地址? [英] How to get the email address of the current logged-in user?

查看:264
本文介绍了如何获取当前登录用户的电子邮件地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是VBA的新手,正在尝试使自动Word文档正常工作.目前,文档中有一个按钮,按下该按钮将触发带有附件的电子邮件.

I'm new to VBA and trying to get an automated word document working. At the moment there is a Button in the document that which upon pressing, will fire off an email with the document attached.

但是,我还需要获取发送电子邮件的当前用户的电子邮件地址,因此我可以将其放置在文档中,然后再发送出去.我在互联网上的搜索结果没有找到符合我情况的可用代码.我当前的代码如下.

However I need to also get the email address of the current user sending the email, so I can place it inside the document before sending it off. My searches on the internet have not resulted in any usable code that meets my situation. My current code is below.

Set OL = CreateObject("Outlook.Application")
Set EmailItem = OL.CreateItem(olMailItem)

Set Doc = ActiveDocument
Doc.Save

With EmailItem
    .Subject = "Requesting Authorization Use Overtime"
    .Body = "Please review the following request for overtime" & vbCrLf & _
    "" & vbCrLf & _
    "Thanks"
    .To = "toemail@test.com"
    .Importance = olImportanceNormal
    .Attachments.Add Doc.FullName
    .Send
End With

不确定是否相关,但是在使用文档时,Outlook应用程序将始终在用户登录的情况下打开.我习惯于在这种情况下获得智能帮助,因此我可以在方法和方法上四处闲逛.属性,但智能感知似乎几乎没有帮助.

Not sure if this is relevant, but when the document is being used, the Outlook application will always be open with a user signed in. Im used to having intellisense help in these sorts of situations so I can fool around with methods and properties, but there seems to be very little help from intellisense.

推荐答案

通常,电子邮件地址是分配给Outlook Mail Folders的名称.
所以试试这个:

Usually, the email address is the name assigned to Outlook Mail Folders.
So try this:

'~~> add these lines to your code
Dim olNS As Outlook.NameSpace
Dim olFol AS Outlook.Folder

Set olNS = OL.GetNamespace("MAPI")
Set olFol = olNS.GetDefaultFolder(olFolderInbox)

MsgBox olFol.Parent.Name '~~> most cases contains the email address

这是假设您正在使用 Early Bind 并正确设置了对象引用.
访问此类信息的另一种方法是直接使用 Namespace 属性.

This is assuming your are using Early Bind with the object reference properly set.
Another way to access such info is directly use Namespace properties.

MsgBox olNS.Accounts.Item(1).DisplayName '~~> usually email address
MsgBox olNS.Accounts.Item(1).SmtpAddress '~~> email address
MsgBox olNS.Accounts.Item(1).UserName '~~> displays the user name

我希望以上任何方式都能有所帮助.

I hope any of the above somehow helps.

这篇关于如何获取当前登录用户的电子邮件地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆