vba 如何获取当前登录用户的电子邮件地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26519325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:05:33  来源:igfitidea点击:

How to get the email address of the current logged-in user?

vbams-wordoutlookword-vbaoutlook-vba

提问by SikhWarrior

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.

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

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 = "[email protected]"
    .Importance = olImportanceNormal
    .Attachments.Add Doc.FullName
    .Send
End With

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 应用程序将始终在用户登录的情况下打开。我习惯于在这些情况下获得智能感知帮助,因此我可以在方法和属性上四处游荡,但是智能感知似乎没有什么帮助。

采纳答案by L42

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

通常,电子邮件地址是分配给 Outlook 邮件文件夹的名称。
所以试试这个:

'~~> 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

This is assuming your are using Early Bindwith the object reference properly set.
Another way to access such info is directly use Namespaceproperties.

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

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.

我希望以上任何一项都会有所帮助。

回答by Dmitry Streblechenko

It all depends on the definition of "the current user address".

这一切都取决于“当前用户地址”的定义。

  1. The address of the primary account in Outlook can be retrieved from Appication.Session.CurrentUser(returns Recipientobject). Use Recipient.Addressproperty. Note however that for an Exchange account (Recipient.AddressEntry.Type == "EX") you will receive an EX type address. To retrieve the SMTP address, use Recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress. Be prepared to handle nulls/exceptions in case of errors. This is what you most likely need in your particular case.

    On the Extended MAPI level (C++ or Delphi), use IMAPISession::QueryIdentity(you can test it in OutlookSpy- click IMAPISession button, then QueryIdentity). You can then read the PR_ADDRTYPEproperty ("EX" vs "SMTP") and PR_EMAIL_ADDRESS(when PR_ADDRTYPE= "SMTP") or (in case of Exchange) PR_SMTP_ADDRESS(not guaranteed to be present) and PR_EMS_AB_PROXY_ADDRESSES(multivalued property will Exchange addresses, including all proxy (alias) addresses).

  2. In case of multiple accounts in the profile, an email can be sent or received through multiple accounts. In that case use MailItem.SendUsingAccount(returns Accountobject, can be null - in that case use Application.Session.CurentUser). This is valid both for received, sent or emails being composed (Application.ActiveInspector.CurrentItemor Application.ActiveExplorer.ActiveInlineResponse).

  3. All accounts in a given profile can be accessed using the Namespace.Accountscollection (Application.Session.Accounts). Account's address can be accessed using Account.SmtpAddressproperty. Note that the Outlook Object Model only exposes mail accounts. Some store accounts (such as PST) are not in the collection since they do not have an intrinsic user identity even if some other accounts (such as POP3/SMTP) can deliver to that store. If you want to access all accounts, you can use Redemptionand its RDOSession.Accounts collection (RDOAccountsobject).

    On the Extended MAPI level, the accounts are exposed through the IOlkAccountManagerinterface. You can play with it in OutlookSpyif you click the IOlkAccountManager button.

  4. In case of delegate Exchange stores, the store owner is not exposed through the Outlook Object Model. You can either use Extended MAPI (note that the PR_MAILBOX_OWNER_ENTRYIDproperty is only exposed by the online store, it is not available in a cached store). You can parse the Exchange store entry id and extract the EX type address from it. You can then construct the GAL object entry id given the EX address. You can also access the store owner using Redemptionand its RDOExchangeMailboxStoreobject and its Ownerproperty.

  1. 可以从Appication.Session.CurrentUser(返回Recipient对象)中检索 Outlook 中主帐户的地址。使用Recipient.Address财产。但是请注意,对于 Exchange 帐户 ( Recipient.AddressEntry.Type == "EX"),您将收到一个 EX 类型的地址。要检索 SMTP 地址,请使用Recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress. 准备好在出现错误时处理空值/异常。这是您在特定情况下最可能需要的。

    在扩展 MAPI 级别(C++ 或 Delphi)上,使用IMAPISession::QueryIdentity(您可以在OutlookSpy 中对其进行测试- 单击 IMAPISession 按钮,然后单击 QueryIdentity)。然后,您可以读取PR_ADDRTYPE属性(“EX”与“SMTP”)和PR_EMAIL_ADDRESS(当PR_ADDRTYPE=“SMTP”)或(在 Exchange 的情况下)PR_SMTP_ADDRESS(不保证存在)和PR_EMS_AB_PROXY_ADDRESSES(多值属性将交换地址,包括所有代理(别名)地址)。

  2. 如果配置文件中有多个帐户,则可以通过多个帐户发送或接收电子邮件。在这种情况下 use MailItem.SendUsingAccount(返回Account对象,可以为 null - 在这种情况下 use Application.Session.CurentUser)。这对于接收、发送或正在编写的电子邮件(Application.ActiveInspector.CurrentItemApplication.ActiveExplorer.ActiveInlineResponse)均有效。

  3. 可以使用Namespace.Accounts集合访问给定配置文件中的所有帐户(Application.Session.Accounts)。可以使用Account.SmtpAddress属性访问帐户地址。请注意,Outlook 对象模型仅公开邮件帐户。某些商店帐户(例如 PST)不在集合中,因为即使某些其他帐户(例如 POP3/SMTP)可以传送到该商店,它们也没有固有的用户身份。如果要访问所有帐户,可以使用Redemption及其RDOSession .Accounts集合(RDOAccounts对象)。

    在扩展 MAPI 级别,帐户通过IOlkAccountManager接口公开。如果单击 IOlkAccountManager 按钮,则可以在OutlookSpy 中使用它。

  4. 在委托 Exchange 存储的情况下,存储所有者不会通过 Outlook 对象模型公开。您可以使用扩展 MAPI(请注意,该PR_MAILBOX_OWNER_ENTRYID属性仅由在线商店公开,在缓存商店中不可用)。您可以解析 Exchange 存储条目 ID 并从中提取 EX 类型地址。然后,您可以根据 EX 地址构造 GAL 对象条目 ID。您还可以使用Redemption及其RDOExchangeMailboxStore对象及其Owner属性访问商店所有者。

回答by James

This answer is for Late Binding so you don't need to have reference libraries. Place the following code in a module:

此答案适用于后期绑定,因此您无需拥有参考库。将以下代码放入模块中:

    Dim OL As Object, olAllUsers As Object, oExchUser As Object, oentry As Object, myitem As Object
    Dim User As String

    Set OL = CreateObject("outlook.application")
    Set olAllUsers = OL.Session.AddressLists.Item("All Users").AddressEntries

    User = OL.Session.CurrentUser.Name

    Set oentry = olAllUsers.Item(User)

    Set oExchUser = oentry.GetExchangeUser()

    msgbox oExchUser.PrimarySmtpAddress