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
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
J Quick steps delete original email and move reply/sent email to folder Using Outlook 2
C Wishlist Extract or scan new email addresses from out of office replies. Leads from OOO replies Using Outlook 1
T External email warning banner Outlook VBA and Custom Forms 0
A Links in email getting error message about group policy Using Outlook 4
richardwing Auto forward email that is moves into a specific outlook folder Outlook VBA and Custom Forms 5
J Recommendations for Outlook Duplicate Email Remover Using Outlook 6
Geldner Tweak Junk Email Reporting tool to default to particular email on send? Using Outlook 3
S Outlook 365 Can I change the possible range of highlighting colours when writing an Outlook email? Using Outlook 1
V Can one change the formatting of email title blocks? Using Outlook 0
P default font when sending email from browser Using Outlook 1
D VBA Macro to Print and Save email to network location Outlook VBA and Custom Forms 1
B IMAP server rejects sent email - cannot deliver messages Using Outlook 2
TedSch Small vba to kill political email Outlook VBA and Custom Forms 3
X Open Hyperlinks in an Outlook Email Message (Help with Diane's solution) Outlook VBA and Custom Forms 3
e_a_g_l_e_p_i Email notifications changed with Outlook 2021 Using Outlook 8
glnz How to retrieve or redo Verizon.net email password without affecting Outlook connection? Using Outlook 1
Z Copy specific email body text Outlook VBA and Custom Forms 0
D ISOmacro to extract active mail senders name and email, CC, Subject line, and filename of attachments and import them into premade excel spread sheet Outlook VBA and Custom Forms 2
M Outlook 365 refuses to send email Using Outlook 1
B Search and Find Email by Folder Name Outlook VBA and Custom Forms 2
K Closing external IMAP email... Outlook 2013 Using Outlook 0
C Email bomb processing Outlook VBA and Custom Forms 1
O What would be the recommended way to change an email address (family member)? Using Outlook 0
A Outlook 2016 Macro to Reply, ReplyAll, or Forward(but with composing new email) Outlook VBA and Custom Forms 0

Similar threads

Top