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