Sorted listing

M

Mike

I am trying to write a VBA program in Word that lists all
of the company names sorted in a list box using my
Outlook address book. When I use the sort statement I
recieve an error. What am I doing wrong in the following
code?

Dim objOutlook As Outlook.Application
Dim fdrContacts As Outlook.MAPIFolder
Dim itmContacts As Outlook.ContactItem
Set objOutlook = New Outlook.Application
Set fdrContacts = GetNamespace("MAPI").GetDefaultFolder _
(olFolderContacts)
For Each itmContacts In fdrContacts.Items.Sort _
("[ComapnyName]")
lstCompanyList.Additem itmContacts. _
ItemProperties(52).Value
Next

lstCompanyList is my ListBox
 
J

Jonathan West

Mike said:
I am trying to write a VBA program in Word that lists all
of the company names sorted in a list box using my
Outlook address book. When I use the sort statement I
recieve an error. What am I doing wrong in the following
code?

Dim objOutlook As Outlook.Application
Dim fdrContacts As Outlook.MAPIFolder
Dim itmContacts As Outlook.ContactItem
Set objOutlook = New Outlook.Application
Set fdrContacts = GetNamespace("MAPI").GetDefaultFolder _
(olFolderContacts)
For Each itmContacts In fdrContacts.Items.Sort _
("[ComapnyName]")
lstCompanyList.Additem itmContacts. _
ItemProperties(52).Value
Next

lstCompanyList is my ListBox

Do you have a typo there? ComapnyName ??
 
M

Mike

Jonathan,

It is a typo in my message but it is correct in my VBA
program. In the program it is .Sort("[CompanyName]").
Why do I get the error message?

Thanks Mike

-----Original Message-----


I am trying to write a VBA program in Word that lists all
of the company names sorted in a list box using my
Outlook address book. When I use the sort statement I
recieve an error. What am I doing wrong in the following
code?

Dim objOutlook As Outlook.Application
Dim fdrContacts As Outlook.MAPIFolder
Dim itmContacts As Outlook.ContactItem
Set objOutlook = New Outlook.Application
Set fdrContacts = GetNamespace ("MAPI").GetDefaultFolder _
(olFolderContacts)
For Each itmContacts In fdrContacts.Items.Sort _
("[ComapnyName]")
lstCompanyList.Additem itmContacts. _
ItemProperties(52).Value
Next

lstCompanyList is my ListBox

Do you have a typo there? ComapnyName ??


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup

.
 
H

Helmut Weber

Hi Mike,
Word-VBA-listboxes as opposed to VB listboxes
do not have a sort methode or sorted property.
At last up to Word 2002.
You may sort it, using this, which is
bubble-sort, very simple, but could
be sufficient. And there are many other ways
of sorting.
---
With .LstLayout
' your list e.g. userform1.listbox1
k = .ListCount
For j = 0 To k - 1
For i = (j + 1) To (k - 1)
If .List(i) < .List(j) Then
aBuf = .List(j) ' abuf = string
.List(j) = .List(i)
.List(i) = aBuf
End If
Next i
Next j
End With

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
G

Guest

Helmut,

I am using Word 2003. I am not positive but I don't
think I am sorting the listbox in Word. I am trying to
sort a Outlook Address Book by CompanyName before putting
the data in the listbox. Before I decided to use the
Outlook Address Book I was using an Access database and
sorted it by CompanyName before putting the data in the
listbox and that worked just fine in Word. Maybe I am
not understanding what is going on but when I typed
fdrContacts.Items. "Sort" was one of the selectable
commands that come up in the box.

Mike
 
G

Guest

Helmut,

While I think the sort should work I still decided to try
your way and it worked just fine. Thanks for your help.

Mike
 

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