Get data from Outlook Contact List?

G

grep

How can I script a document to pull contact information from my Outlook
Contact List?

grep
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < grep > écrivait :
In this message said:
How can I script a document to pull contact information from my Outlook
Contact List?

I am not an expert on Outlook VBA, but here is an example I concocted some
time ago for a client.
Watch out for wrapping, if you paste it in the VBE and get redlines, it
means you have to bring the second red line back at the end of the preceding
red line.
Also, I use early binding, so you have to set a reference to the Outlook
Object Library (in the VBE, Tools > References and check the box next to
Microsoft Outlook 10.0 Object Library, or a different number if you have a
different version)
This was designed for Word/Outlook 2000.
This example pulls a list of contacts that belong to a certain category and
populates a list box on a userform.

For more info, here is a great site full of resources regarding Outlook:

http://www.slipstick.com/outlook/index.htm

'_______________________________________
'Rename the sub if you are using it in a regular module
Private Sub UserForm_Initialize()

'I only wanted a certain category of contacts
Const olCategory = "Facturation"

Dim olApp As Outlook.Application
Dim mycontact As ContactItem
Dim objContacts As MAPIFolder
Dim objNameSpace As NameSpace
Dim ContactNumber As Integer
Dim myCount As Long

Set olApp = CreateObject("Outlook.Application")
Set objNameSpace = olApp.GetNamespace("MAPI")
Set objContacts = objNameSpace.GetDefaultFolder(olFolderContacts)

ContactNumber = 0
For Each myItem In objContacts.Items
If myItem.Class = 40 Then '40 = contactItem 69 = DistibListItem
Set mycontact = myItem

'Remove this [If .. End If ] if you want all contacts
'Regardless of category
'[
If mycontact.Categories = olCategory Then
ContactNumber = ContactNumber + 1
End If
']
End If
Set mycontact = Nothing
Next

'Create a dynamic array variable, and then declare its initial size
Dim MyListArray() As String
ReDim MyListArray(ContactNumber - 1, 1)

For Each myItem In objContacts.Items
If myItem.Class = 40 Then '40 = contactItem 69 = DistibListItem
Set mycontact = myItem
If mycontact.Categories = olCategory Then
MyListArray(myCount, 0) = mycontact.CustomerID
MyListArray(myCount, 1) = mycontact.CompanyName _
& " (" & mycontact.FirstName & " " & mycontact.LastName
& ")"
myCount = myCount + 1
End If
Set mycontact = Nothing
End If
Next

'sort the array: ascending, first, last, by rows, column #2 as key
WordBasic.SortArray MyListArray(), 0, 0, ContactNumber - 1, 0, 1

Set olApp = Nothing
Set objNameSpace = Nothing
Set objContacts = Nothing

'If you want to type the list in a document
'ignore the following and just
'convert the array to a string
'and insert it in the document

'Set the number of columns in the listbox
With CoToBillCbox
.ColumnCount = 2
.BoundColumn = 1
.TextColumn = 2
.ColumnWidths = "26;"
.List() = MyListArray
End With

End Sub
'_______________________________________

Good luck!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

Malcolm Smith

Jean-Guy

As a matter of interest (I am not trying to pick fault here) but would
that pick out any contents in sub-folders?

Regards
- Malc
 
J

Jean-Guy Marcil

Bonjour,

Dans son message, < Malcolm Smith > écrivait :
In this message said:
Jean-Guy

As a matter of interest (I am not trying to pick fault here) but would
that pick out any contents in sub-folders?

You mean subfolders in the Outlook Contact folder?

If so, I do not know as I have never had a sub-folder in the Contacts
folder. It would have to be tested. It probably depends on how Outlook
determine that an Item is a contact. Is there a type associated with the
entry in its internal database that labels it as a contact, in which case I
guess all contacts would be picked up, regardless of their location. Or, if
contact are merely contacts for being in the Contacts folder, then I do not
know what would happen to "contact" not directly in the Contacts folder.

If not, I am afraid I do not understand your question!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
M

Malcolm Smith

Jean-Guy

You ought to see my contacts folder! Sub folders all over the place and I
think that the code will only pick up the contacts in the main folder.
You will have to get all of the Folders in that collection and then with a
recursive function get the whole lot.

A good exercise for our humble readers; them learning to write recursive
functions without going grey. :)

- Malc
www.dragondrop.com
 

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

Top