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
 
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
 
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
 
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
 
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
 
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
T Why do Outlook Desktop 2021 tasks from my wife's email show up in my task pane? Using Outlook 2
A Opening a link from an email automatically Outlook VBA and Custom Forms 0
D Outlook 2021 New email reminder Using Outlook.com accounts in Outlook 1
Rupert Dragwater How do I remove an email ending with @gmail.com Using Outlook 4
M A plug in (or method) to keep email message formatting after it expires Using Outlook 1
L VBA to Triage Incoming Email Outlook VBA and Custom Forms 0
R Legacy Outlook on Mac Email Cache Using Outlook 0
P Email address auto-completes work fine on laptop, but no longer on desktop Using Outlook 3
S Create Outlook Task from Template and append Body with Email Body Outlook VBA and Custom Forms 4
H Copying email address(es) in body of email and pasting in To field Outlook VBA and Custom Forms 1
A Search folder and move the email Outlook VBA and Custom Forms 0
P VBA to add email address to Outlook 365 rule Outlook VBA and Custom Forms 0
farrissf Outlook 2016 Optimizing Email Searches in Outlook 2016: Seeking Insights on Quick Search vs Advanced Search Features Using Outlook 0
D Delete selected text in outgoing email body Outlook VBA and Custom Forms 0
F Graphics in email / Mac recipient garbled Using Outlook 0
D Outlook VBA forward the selected email to the original sender’s email ID (including the email used in TO, CC Field) from the email chain Outlook VBA and Custom Forms 2
Witzker Outlook 2019 Macro to seach in all contact Folders for marked Email Adress Outlook VBA and Custom Forms 1
E Outlook 365 Save Selected Email Message as .msg File - oMail.Delete not working when SEARCH Outlook VBA and Custom Forms 0
S Email Macros to go to a SHARED Outlook mailbox Draft folder...NOT my personal Outlook Draft folder Using Outlook 2
R Outlook 365 VBA AUTO SEND WITH DELAY FOR EACH EMAIL Outlook VBA and Custom Forms 0
G Print email attachments when hit subfolder Outlook VBA and Custom Forms 1
C Spam Email? Using Outlook 2
G Automatically delete email when a condition is met Outlook VBA and Custom Forms 1
E Save Selected Email Message as .msg File - digitally sign email doesn't works Outlook VBA and Custom Forms 1
S Email was migrated from GoDaddy to Microsoft exchange. We lost IMAP ability Exchange Server Administration 1
R Outlook 365 How to integrate a third-party app with Outlook to track email and sms? Using Outlook 2
S Paperclip icon shows without attachment in email under Sent folder Using Outlook 0
B Outlook 2019 Automatically move email after assigning category Using Outlook 4
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 4
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 3

Similar threads

Back
Top