How to return your outlook account email as a variable in VBA?

thardin

Board Regular
Joined
Sep 29, 2021
Messages
137
Office Version
  1. 365
Platform
  1. Windows
For some reason at work, my Excel Account email and my Outlook Account emails are slightly different.
My Excel email uses myworkID@***.com and I return that using Environ$("username") and then I set the variable :
My_Email = Environ$("username") & "@***.com"
However my Outlook Account email is myFirstName.LastName@****.com
If I tried sending myself an email addressing it to my Excel Account email, it would work, but when I run this code, it doesn't work( and I found out it's because I need to use the FirstName.LastName@***.com variable from outlook. )
So How would I return my outlook account Email and set it as a variable in Excel?
Thanks


VBA Code:
Sub Open_Most_Recent_Email()
   
    Dim OlApp As Object
    Dim OlMail As Object
    Dim OlItems As Object
    Dim OlFolder As Object
    Dim i As Long

    If Err <> 429 Then    
        Set OlApp = CreateObject("Outlook.Application")
    Else
        Set OlApp = GetObject(, "Outlook.Application")
    End If

    Set OlFolder = OlApp.GetNamespace("MAPI").Folders(Environ$("username") & "@***.com").Folders("abc")
    Set OlItems = OlFolder.Items
    i = OlItems.Count
    
    OlFolder.Items(i).Display
     
    Set OlFolder = Nothing
    Set OlItems = Nothing
    Set OlMail = Nothing
    Set OlApp = Nothing

End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.

Forum statistics

Threads
1,215,832
Messages
6,127,151
Members
449,366
Latest member
reidel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top