D NFINITY
D NFINITY

Reputation: 25

Get the signed-in Office 365 user id

How can I get the signed-in Office 365 user's id in Excel VBA?

Not the the Application.Username or Environ("username")

Upvotes: 1

Views: 2845

Answers (2)

Eugene Astafiev
Eugene Astafiev

Reputation: 49435

The Office extensibility model doesn't provide anything for that.

The best what you can do is to log in to the Outlook profile which corresponds to the Office account and use the GetExchangeUser method which returns an ExchangeUser object that represents the AddressEntry if the AddressEntry belongs to an Exchange AddressList object such as the Global Address List (GAL) and corresponds to an Exchange user. You have to be connected to the Exchange server to use this method. And nobody can guarantee the default profile is connected to the Office365 account in Outlook.

Upvotes: 2

jblack
jblack

Reputation: 576

I'm assuming by "ID" you're referring to the name of the person? The following should work

Sub getLoggedInUser()

Dim olApp As Object
Dim olNS As Object

Set olApp = CreateObject("Outlook.Application")
Set olNS = olApp.GetNamespace("MAPI")

    With olNS.session.currentuser.AddressEntry.GetExchangeUser
        'some different options for getting the name
        Debug.Print .Name
        Debug.Print .FirstName
        Debug.Print .LastName
        Debug.Print .Alias
    End With

End Sub

Upvotes: 2

Related Questions