Solved

VB Code To Loop Through Sent Items And Extract Recipients Email Addresses

Posted on 2006-10-24
3
749 Views
Last Modified: 2012-06-27
Hi,
I need a quick way of scanning a users sent items and extracting all the recipient email addresses (as strings) and saving them to a database. I'm not used to VB app programming and am having real trouble finding a way of extracting the recipient email address(es) as a string from a mailitem object. Basically I can't find the right property of the mailitem object - I have tried the recipient (and recipient.addressitem) property but this doesn't work - Run time error '438', Object doesn't support this property or method. I suspect this is because the property is not of a string type and the variable and the email_addresses variable is. I just can't find the property I need in any of the help files! Or don't know how to convert the property to a string. I'm using the following code (I will add in the code to extract and save to the DB later).

Sub HarvestSent()
 Dim myOlApp As Outlook.Application
 Dim SentItems As Outlook.MAPIFolder
 Dim obj As Outlook.MailItem
 Dim email_addresses As String
 Dim i As Integer
 Set myOlApp = CreateObject("Outlook.Application")
 Set SentItems = myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail)
 For i = 1 To SentItems.Items.Count
  If SentItems.Items(i).Class = olMail Then
   Set obj = SentItems.Items.Item(i)
    email_addresses = obj.Recipient            'this is where it fails
  End If
 Next
End Sub

Please help this is doing my head in!!!

Thanks.
0
Comment
Question by:ShinyApples
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
mvidas earned 500 total points
ID: 17795781
Shiny Apples,

Bear in mind, this will likely trigger the outlook security warning.  Consider using the Redemption object instead to avoid it: http://www.dimastr.com/redemption/home.htm

But you need to get the .address of the .recipient

Sub HarvestSent()
 Dim myOlApp As Outlook.Application
 Dim SentItems As Outlook.MAPIFolder
 Dim email_addresses As String
 Dim i As Integer, j As Integer
 Set myOlApp = CreateObject("Outlook.Application")
 Set SentItems = myOlApp.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail)
 For i = 1 To SentItems.Items.Count
  If TypeName(SentItems.Items(i)) = "MailItem" Then
   For j = 1 To SentItems.Items.Item(i).Recipients.Count
    email_addresses = SentItems.Items.Item(i).Recipients(j).Address
    Debug.Print email_addresses
   Next
  End If
 Next
 Set SentItems = Nothing
 Set myOlApp = Nothing
End Sub

If you want the name of the recipient, use .AddressEntry

Matt
0
 

Author Comment

by:ShinyApples
ID: 17796842
Thank you very much matt that worked perfectly. The points are yours!

0
 
LVL 35

Expert Comment

by:mvidas
ID: 17796857
Glad to help!
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Follow this checklist to learn more about the 15 things you should never include in an email signature from personal quotes, animated gifs and out-of-date marketing content.
This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question