Extracting E-mail adddresses from Outlook



I would like to use the following code as a basis for programmatically
extracting e-mail addresses from my Outlook 2003 to feed to a Pegasus e-mail
client. (I don't like the fact that Outlook displays each recipient's name
in a distribution list.)

The code fails on a “type mismatch†when it reads a Distribution List in my
contacts. I tried looping through ContactItems instead of Contacts, but that
didn’t help. I will readily admit the concept of objects is still a little
fuzzy to me.

Does anyone know a way around this?


Dim ol As Object
Dim olns As Object
Dim objFolder As Object
Dim objAllContacts As Object
Dim Contact As Object
Dim myOlApp As Outlook.Application
Dim myItem As Outlook.ContactItem
Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.CreateItem(olContactItem)

' Set the Application object.
Set ol = New Outlook.Application

' Set the Namespace object.
Set olns = ol.GetNamespace("MAPI")

' Set the default Contacts folder.
Set objFolder = olns.GetDefaultFolder(olFolderContacts)

' Set objAllContacts equal to the collection of all contacts.
Set objAllContacts = objFolder.Items

' Loop through each contact.
For Each Contact In objAllContacts

Set myItem = Contact
' Display the Fullname field for the contact.
If myItem.Categories = "Personal" _
And myItem.Email1Address <> "" Then
MsgBox myItem.Email1Address
End If




Michael Bauer

Am Sun, 5 Feb 2006 20:19:28 -0800 schrieb Robert:

After the For Each line you need to check the object´s type. Because myItem
is declared as ContactItem the line will fail on DistListItems.

This works:

If TypeOf Contact is Outlook.ContactItem Then
Set myItem=Contact

BTW: If you send the mail to yourself and set the DL into the BCC field then
no recipient will see any other name.




Thank you - that did work.

Your suggestion for using the BCC field is a good one. It would save me the
trouble of having to extract the names and addresses.


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question