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

后端 未结 3 1218
悲&欢浪女
悲&欢浪女 2020-12-17 21:12

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 t

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-17 21:28

    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
    

    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.

提交回复
热议问题