Get email addresses of a list of users using outllook

Tejas Kore

Board Regular
Joined
Nov 2, 2017
Messages
72
Office Version
  1. 365
Platform
  1. Windows
Hello Friends,

I am trying get email addresses of a list of users present in Sheet1:C. User's names are like: "FirstName LastName" eg. John Smith.

First iteration completes but 2nd iteration giving error. Please find the error screenshot attached.

Following is my VBA code:
VBA Code:
Sub Fetch_Emailaddress()
  

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim i As Integer
    Dim lrow_A As Integer

    Set wb = ThisWorkbook
    Set ws = wb.Sheets("Sheet1")

    lrow_A = ws.Range("A" & Rows.Count).End(xlUp).Row
   
    Dim oout As Object
    'Dim oout As Outlook.Application
    Dim ns As Namespace
    Dim flag As Boolean
    Dim to_get_emailaddress_Addr As Variant
    Dim to_get_emailaddress_ExUser As Variant
   
    Set oout = CreateObject("Outlook.Application")
    Set ns = oout.GetNamespace("MAPI")
   
    For i = 2 To lrow_A
        flag = True
        Set to_get_emailaddress = ns.CreateRecipient(ws.Cells(i, 3))
        to_get_emailaddress.Resolve
'        ws.Cells(i, 4) = to_get_emailaddress.AddressEntry.GetExchangeUser.PrimarySmtpAddress
        'Debug.Print to_get_emailaddress.AddressEntry
        Set to_get_emailaddress_Addr = to_get_emailaddress.AddressEntry
        Debug.Print i
        Debug.Print to_get_emailaddress_Addr.Type --> Getting run time error here
'        If to_get_emailaddress_Addr Is Nothing Then
'            flag = False
'        Else
'            ws.Cells(i, 4) = to_get_emailaddress_Addr.GetExchangeUser.PrimarySmtpAddress
'        End If
        'MsgBox to_get_jobtitle_ExUser.JobTitle
'        If flag Then
'            Cells(i, 4) = to_get_emailaddress_ExUser.PrimarySmtpAddress
'        End If
'        Set to_get_emailaddress = Nothing
'        Set to_get_emailaddress_Addr = Nothing

    Next i
End Sub

Getting error here -->

Any help would be appreciated.

Thanks !!
 

Attachments

  • Capture.PNG
    Capture.PNG
    7.1 KB · Views: 7

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hello Friends,

I am trying get email addresses of a list of users present in Sheet1:C. User's names are like: "FirstName LastName" eg. John Smith.

First iteration completes but 2nd iteration giving error. Please find the error screenshot attached.

Following is my VBA code:
VBA Code:
Sub Fetch_Emailaddress()
 

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim i As Integer
    Dim lrow_A As Integer

    Set wb = ThisWorkbook
    Set ws = wb.Sheets("Sheet1")

    lrow_A = ws.Range("A" & Rows.Count).End(xlUp).Row
  
    Dim oout As Object
    'Dim oout As Outlook.Application
    Dim ns As Namespace
    Dim flag As Boolean
    Dim to_get_emailaddress_Addr As Variant
    Dim to_get_emailaddress_ExUser As Variant
  
    Set oout = CreateObject("Outlook.Application")
    Set ns = oout.GetNamespace("MAPI")
  
    For i = 2 To lrow_A
        flag = True
        Set to_get_emailaddress = ns.CreateRecipient(ws.Cells(i, 3))
        to_get_emailaddress.Resolve
'        ws.Cells(i, 4) = to_get_emailaddress.AddressEntry.GetExchangeUser.PrimarySmtpAddress
        'Debug.Print to_get_emailaddress.AddressEntry
        Set to_get_emailaddress_Addr = to_get_emailaddress.AddressEntry
        Debug.Print i
        Debug.Print to_get_emailaddress_Addr.Type --> Getting run time error here
'        If to_get_emailaddress_Addr Is Nothing Then
'            flag = False
'        Else
'            ws.Cells(i, 4) = to_get_emailaddress_Addr.GetExchangeUser.PrimarySmtpAddress
'        End If
        'MsgBox to_get_jobtitle_ExUser.JobTitle
'        If flag Then
'            Cells(i, 4) = to_get_emailaddress_ExUser.PrimarySmtpAddress
'        End If
'        Set to_get_emailaddress = Nothing
'        Set to_get_emailaddress_Addr = Nothing

    Next i
End Sub

Getting error here -->

Any help would be appreciated.

Thanks !!
Apologies for the inconvenience. There were few names which were not able to resolve or had multiple entries in the addresslist hence it was giving runtime error. I added "On error Resume Next" to avoid the error.
 
Upvote 0
Solution

Forum statistics

Threads
1,214,987
Messages
6,122,618
Members
449,092
Latest member
amyap

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