Capture email addresses and create a comma separated list

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
 
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
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
L Checking Sender Email Address for trusted domain from list on intranet Outlook VBA and Custom Forms 4
J How do you disable address search box when typing @ in body of email? Using Outlook 0
S HTML Code Embedded in String Within Open Outlook Email Preventing Replace(Application.ActiveInspector.CurrentItem.HTMLBody From Working Outlook VBA and Custom Forms 4
Victor.Ayala Automated way to check the option "Show this folder as an email Address Book" Outlook VBA and Custom Forms 2
D Wrong email address in Outlook 2003 "From" tab in new outgoing emails Using Outlook 4
D Forwarding email based on the attachment file type and specific text found on the attachment file name Outlook VBA and Custom Forms 1
F Forward incoming email with 4 embedded images in the body without original sender Outlook VBA and Custom Forms 22
W Macro to Filter Based on Latest Email Outlook VBA and Custom Forms 6
D Create advanced search (email) via VBA with LONG QUERY (>1024 char) Outlook VBA and Custom Forms 2
C Outlook 2007 Removing then adding account restores junk email processing Using Outlook 0
G Place jpg in body of email Outlook VBA and Custom Forms 1
F Wishlist Outlook suddenly began synchronizing deleted items every time I delete a single email. Using Outlook 2
N Save Selected Email Message as .msg File Outlook VBA and Custom Forms 12
HarvMan Toggle between calendar and email in Outlook 365 Using Outlook 12
F Email being marked as Spam by Gmail and not being visible in Outlook Using Outlook 5
G Email time stamp Using Outlook 2
G Schedule recurring email and attachments display Outlook VBA and Custom Forms 3
G Save and Rename Outlook Email Attachments Outlook VBA and Custom Forms 0
B Need to Copy an email to a subfolder Outlook VBA and Custom Forms 2
M How to setup outlook after importing old account information - Entering email account info creates with "(1)" after the account! Using Outlook 1
K Multiple Rules on Single Email Using Outlook 2

Similar threads

Top