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: 8

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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,223,942
Messages
6,175,544
Members
452,652
Latest member
eduedu

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