Capture email addresses and create a comma separated list

Status
Not open for further replies.

leeford

Member
Outlook version
Outlook 365 64 bit
Email Account
Exchange Server
Hello,

I'm not all that great at creating a script from scratch but have been pretty good at modifying one to meet my needs when I find a similar example.
I'm trying to find a way to capture all the email addresses that are in all of the email address fields of a mail item that I have open, then output only the email addresses in a comma separated list. It could be to the console as dialogue box with content or it could be exported as a csv.

This seems like it would be an easy task, but again, I'm not all that great at creating something from scratch. Programming is not my area of expertise.

Any chance someone would be able to give me an example of a way I could do this?

Thanks in advance,
Lee
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
This one shows how to get the addresses - it does it on sent messages, but you only need the "meat" in the middle.

not tested, but this should work as long as I did not make a typo.
Code:
Dim Recipients As Outlook.Recipients
Dim R As Outlook.Recipient
Dim strR as string

Set Recipients = Item.Recipients
For i = Recipients.count To 1 Step -1

Set R = Recipients.Item(i)

strR = R & "; " & strR

next i

'do whatever 
msgbox strR
debug.print strR

This shows how to run a macro on selected messages or all messages in a folder
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Couldn't resist testing it - you need to use .Address - otherwise you get the display name. And you won't get the sender... unless you add the senderemailaddress field.

If "do whatever" is inside the next block, it will do it after each message - if its after the next block, it will keep adding addresses to the string.

Because this is all within the With obj code in my selected messages sample, you don't need the obj name name, just the . and field name,

Code:
      Set R = Recipients.Item(i)
       Debug.Print R.Address
      strR = R.Address & "; " & strR

        Next i

   strR = .SenderEmailAddress & "; " & strR
     
     End With
 

leeford

Member
Outlook version
Outlook 365 64 bit
Email Account
Exchange Server
Thank you. I was able to get pretty far and was able to get some output to the message box that I could then copy out, but I didn't get the email addresses, I got what looked like exchange folder locations, instead. What did I do wrong? It looks like I need to get the exchange user now, but I keep running into a lot of different errors when I am trying to get the exchange user.

This is what I got written so far, but it returns the exchange user object, I think. How do I get the email address from that?

Code:
Sub GetALLEmailAddresses()

Dim Item As Outlook.MailItem
Dim Recipients As Outlook.Recipients
Dim R As Outlook.Recipient
Dim strR As String

Set Item = Application.ActiveInspector.CurrentItem


Set Recipients = Item.Recipients
With Recipients
    For i = Recipients.Count To 1 Step -1
      Set R = Recipients.Item(i)
       Debug.Print R.Address
       strR = R.Address & "; " & strR

    Next i

   strR = strR & "; " & Item.SenderEmailAddress
  
End With
'do whatever
If MsgBox(strR, vbInformation, "Email Addresses") = vbOK Then
Cancel = True
End If
Debug.Print strR
End Sub
 

leeford

Member
Outlook version
Outlook 365 64 bit
Email Account
Exchange Server
I finally got it. Had to do a little more googling, but I found and modified something that works. I have zero error handling, so hopefully it works in all cases, but it has at least worked in the examples that I've tried it on.

I also found a script that copies my string to the clipboard, which is the function at the end.
If anyone can find an issue with it, please let me know so I can fix it.

Thank you for getting me down the road. I appreciate the help.

Here is my working code:
Code:
Sub GetSMTPAddressForAll()
    
    Dim recips As Outlook.Recipients
    Dim recip As Outlook.Recipient
    Dim pa As Outlook.PropertyAccessor
    Dim strR As String
    Dim Response As VbMsgBoxResult
    Dim mail As Outlook.MailItem
        
    Const PR_SMTP_ADDRESS As String = _
        "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
        
    Set mail = Application.ActiveInspector.CurrentItem

    Set recips = mail.Recipients
    With recips
        For Each recip In recips
            Set pa = recip.PropertyAccessor
            Debug.Print recip.Name & " SMTP=" & pa.GetProperty(PR_SMTP_ADDRESS)
            strR = pa.GetProperty(PR_SMTP_ADDRESS) & "," & strR
        Next
    
    End With
    strR = strR & mail.Sender.PropertyAccessor.GetProperty(PR_SMTP_ADDRESS)
    
    Clipboard (strR)

End Sub


Function Clipboard(Optional StoreText As String) As String
'PURPOSE: Read/Write to Clipboard
'Source: ExcelHero.com (Daniel Ferry)

Dim x As Variant

'Store as variant for 64-bit VBA support
  x = StoreText

'Create HTMLFile Object
  With CreateObject("htmlfile")
    With .parentWindow.clipboardData
      Select Case True
        Case Len(StoreText)
          'Write to the clipboard
            .setData "text", x
        Case Else
          'Read from the clipboard (no variable passed through)
            Clipboard = .GetData("text")
      End Select
    End With
  End With

End Function
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Yeah, if you use exchange, you need to look up the SMTP for internal users.

Or filter them out. This is how I did it when I was testing the macro

Code:
     With obj
    ' do whatever
       Debug.Print .Subject, .SenderEmailAddress
  
      Set Recipients = .Recipients
      For i = Recipients.Count To 1 Step -1

      Set R = Recipients.Item(i)
       Debug.Print UCase(R.Address)
    If InStr(1, UCase(R.Address), "/OU") = 0 Then
      strR = R.Address & "; " & strR
    End If

        Next i
 If InStr(1, UCase(.SenderEmailAddress), "/OU") = 0 Then
   strR = .SenderEmailAddress & "; " & strR
 End If
 
     End With

You can also add the string to a new message body.

Code:
Set olMsg = Application.CreateItem(olMailItem)
With olMsg
.Subject = Now ' the current time and date
    .BodyFormat = olFormatPlain
    .Body = strR 
    .Display
End With
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
T Some way to capture who marks an email as read Outlook VBA and Custom Forms 1
R Capture Sender's Display name and Address Outlook VBA and Custom Forms 3
M Using field names to capture a data element Using Outlook 0
I Capture incoming emails Outlook VBA and Custom Forms 1
A Capture network traffic in IE to file Outlook VBA and Custom Forms 5
JorgeDario How to capture and save the text, when responding a MailItem? Outlook VBA and Custom Forms 3
G Capture "forward event" ? Outlook VBA and Custom Forms 11
J VBA Form in Outlook to capture data and complile a mail Outlook VBA and Custom Forms 2
A how to capture events for next item Outlook VBA and Custom Forms 10
B Capture Inbound E-mail Subject with VBA Outlook VBA and Custom Forms 4
R How to capture a Mailitem Event Outlook VBA and Custom Forms 3
A how to capture outlook 2007 events Outlook VBA and Custom Forms 4
C Event Capture - Item Context Menu (Delete). Can you hook it? Outlook VBA and Custom Forms 1
Rupert Dragwater How to permanently remove an email address Using Outlook 9
K vba code to auto download email into a specific folder in local hard disk as and when any new email arrives in Inbox/subfolder Outlook VBA and Custom Forms 0
F Auto changing email subject line in bulk Using Outlook 2
F Want to add second email to Outlook for business use Using Outlook 3
kburrows Outlook Email Body Text Disappears/Overlaps, Folders Switch Around when You Hover, Excel Opens Randomly and Runs in the Background - Profile Corrupt? Using Outlook 0
J Outlook 365 Outlook Macro to Sort emails by column "Received" to view the latest email received Outlook VBA and Custom Forms 0
A Outlook 2019 Help with forwarding email without mentioning the previous email sender. Outlook VBA and Custom Forms 0
J Macro to send email as alias Outlook VBA and Custom Forms 0
M Shift Delete doesn't delete email from server Using Outlook 4
K Incorporate selection from combobox into body of email Outlook VBA and Custom Forms 0
L Why are some email automatically going to "archive" Using Outlook 2
M Outlook Macro to save as Email with a file name format : Date_Timestamp_Sender initial_Email subject Outlook VBA and Custom Forms 0
B Outlook 2019 Custom Email form - Edit default email form Outlook VBA and Custom Forms 6
F Add a category before "Send an Email When You Add an Appointment to Your Calendar" Outlook VBA and Custom Forms 0
T Problem when requesting to view an email in a browser Using Outlook 0
J Outlook 365 Forward Email Subject to my inbox when new email arrive in shared inbox Using Outlook 0
HarvMan Archive Email Manually Using Outlook 1
L Fetch, edit and forward an email with VBA outlook Outlook VBA and Custom Forms 2
S New Email "From" box stopped working Using Outlook 0
Rupert Dragwater Duplicate email in Folder Using Outlook 8
M "Attachment Detacher for Outlook" add in, does it update the server copy of the email? Using Outlook 1
W Outlook 365 I am getting the "Either there is no default mail client" error when I try to send an email on excel Office 365 Using Outlook 1
MattC Changing the font of an email with VBA Outlook VBA and Custom Forms 1
L Specific Incoming Email Address Immediately Deleted Using Outlook 2
Witzker Outlook 2019 Macro to send an Email Template from User Defined Contact Form Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Edit contact from email does not open the user defined contactform Using Outlook 3
V Macro to mark email with a Category Outlook VBA and Custom Forms 4
R Roadrunner Email Settings | Contact Roadrunner Customer Support Outlook VBA and Custom Forms 1
D Gmail mail is being delivered to a different email inbox in Outlook App 2021 Using Outlook 2
Albert McCann Outlook 2021 Outlook Display of HTML Email from two senders is glitchy Using Outlook 0
A How Do I Setup My Optonline.Net Email Account? Outlook VBA and Custom Forms 1
H Preventing the 'email address fetch from Exchange' crashing email reading code Exchange Server Administration 0
D multiple email accounts - why do I have to choose the "from" account address?? Using Outlook 2
Wotme create email only data file Using Outlook 1
F Outlook 2016 Email with attachments not being received Using Outlook 3
J Outlook 2019 Regex email addresses from body Outlook VBA and Custom Forms 6
D Prompt to prefix subject line whenever sending an email Outlook VBA and Custom Forms 3

Similar threads

Top