How do I get the email address of the current registered user?

I am new to VBA and am trying to work with an automatic document. At the moment, the document has a button that, after clicking, will fire an email with the attached document.

However, I also need to get the email address of the current user sending the email, so I can put it in the document before sending it. My searches on the internet have not resulted in any usable code that suits 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

      

Not sure if this is relevant, but when the document is in use, Outlook will always be open with the user's subscriber. I used to have intellisense help in situations like this so that I can spoof with methods and properties, but there seems to be very little help from intellisense.

+7


source to share


3 answers


Typically, 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

      

This assumes you are using Early Bind with the correct object reference set.
Another way to access such information is by directly using 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

      

Hope some of the above helps.

+6


source


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

  1. Primary account address in Outlook can be obtained from Appication.Session.CurrentUser

    (returns object Recipient

    ). Use the property Recipient.Address

    . Note, however, that for the Exchange ( Recipient.AddressEntry.Type == "EX"

    ) account, you will receive an EX address. To get 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 specific case.

At the advanced MAPI level (C ++ or Delphi) use IMAPISession::QueryIdentity

(you can check this in OutlookSpy - click the IMAPISession button, then QueryIdentity). Then you can read properties PR_ADDRTYPE

("EX" versus "SMTP") and PR_EMAIL_ADDRESS

(when PR_ADDRTYPE

= "SMTP") or (in the case of Exchange) PR_SMTP_ADDRESS

(presence not guaranteed) and PR_EMS_AB_PROXY_ADDRESSES

(multivalued property would be Exchange addresses, including all proxy (alias) addresses).



  1. In the case of multiple accounts in a profile, the email can be sent or received through multiple accounts. In this case use MailItem.SendUsingAccount

    (returns an object Account

    , can be null - in this case use Application.Session.CurentUser

    ). This is valid as for received, sent or composed letters ( Application.ActiveInspector.CurrentItem

    or Application.ActiveExplorer.ActiveInlineResponse

    ).

  2. All accounts in this profile can be accessed using the collection Namespace.Accounts

    (Application.Session.Accounts

    ). The account address can be obtained using the property Account.SmtpAddress

    . Note that the Outlook object model only provides mail accounts. Some store accounts (eg PST) are missing from the collection because they do not have internal user credentials, even though some other accounts (eg POP3 / SMTP) may be delivering to this store. If you want to access all accounts, you can use Redemption and its RDOSession .Accounts collection ( RDOAccounts object ).

At the extended MAPI level, accounts are opened through the IOlkAccountManager interface . You can play with it in OutlookSpy by clicking the IOlkAccountManager button.

  1. For Exchange delegated stores, the store owner is not exposed through the Outlook object model. You can use Extended MAPI (note that the property PR_MAILBOX_OWNER_ENTRYID

    is only available in the online store, it is not available in cached storage). You can parse the ID of the Exchange store entry and extract the EX address from it. Then you can create a GAL Entity Record ID at EX. You can also access the store owner using Redemption and its RDOExchangeMailboxStore object and its properties Owner

    .
+8


source


This answer is for late linking, 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

      

+3


source







All Articles