excelvbaoutlookexchange-server-2010

Get current user - signed to exchange server - manager email address


How can I retrieve the current user manager email address? I'm only able to get my e-mail address by calling the

Application.Session.CurrentUser.AddressEntry.GetExchangeUser

while using

Application.Session.CurrentUser.AddressEntry.GetExchangeUser.Manager

returns nothing... What i'm trying to achieve is to send an email to my manager right from excel sheet and automate it on other users computers (because they've other managers)

Sub Mail_workbook_Outlook_1()
    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    If IsEmpty(Range("A21").Value) = True Then ' check if cell is empty
      MsgBox "Cell is empty!", vbCritical, "It would be better to not left it empty..." ' if so, show user a message
    Else

    On Error Resume Next
    With OutMail ' outlook message details
        .To = "xyz@xyz.net"
        .Subject = "Sales report - " & Format(Date, "dd-mm-yyyy")
        .Body = "Here comes the full sales report from " & Format(Date, "dd-mm-yyyy") & Application.Session.CurrentUser.AddressEntry.GetExchangeUser
        .Attachments.Add ActiveWorkbook.FullName ' add current file as an attachment to the outlook message
        .Send
    End With
    On Error GoTo 0
    MsgBox "File sent ", vbInformation, "You can now safely close the report" ' show confirmation message

    Set OutMail = Nothing
    Set OutApp = Nothing
    End If
End Sub

Outlook contact with manager


Solution

  • To get current User's Manager.

    Example on

    Option Explicit
    Sub GetManager()
    
        MsgBox CreateObject("Outlook.Application").GetNamespace("MAPI") _
                        .CurrentUser.AddressEntry.GetExchangeUser.Manager
    
    End Sub
    

    Edit

    Remember user must have manager setup.

    Modify your code this way

        With OutMail ' outlook message details
            .To = CreateObject("Outlook.Application").GetNamespace("MAPI") _
                        .CurrentUser.AddressEntry.GetExchangeUser.Manager
    
            .Subject = "Sales report - " & Format(Date, "dd-mm-yyyy")
            .Body = "Here comes the full sales report from " & Format(Date, "dd-mm-yyyy")
            .Attachments.Add ActiveWorkbook.FullName 'attachment File 
            .Display
    '        .Send
        End With
    

    Also See on MSDN Example

    How to: Get Availability Information for an Exchange User's Manager