Populating Combo Boxes.

D

Dave

I'm really new to outlook and vbScript. Fortunately, not new to
programming.

I've created a new form ("Houses") which is based off of a contact
form.
In that form I have 8 fields of different Contacts. All of those
contacts ought to be in the contacts folder.

For Example, the form contains the fields "Buyer, Seller, Builder,
etc..."

I want to be able to select the name of a contact from a drop down
list of all contacts for each one and then automatically add those
names to the Contacts field in the form so that it will link to the
item automatically.

My problen is that when I auto populate the combo box, my list is out
of order and incomplete. Help me.

I've tried cut n pasting several peoples code.. this is what is
running now...

Sub Item_Open()

' Sets FormPage equal to the page ("General")
Set FormPage = Item.GetInspector.ModifiedFormPages("General")

' Sets Control to a ListBox called ComboBox1
Set Control = FormPage.Controls("ComboBox1")

' Assign values to the ComboBox from Outlook Contacts

' Sets an object for the application
Set olns = Application.Session
' Get the default Contacts folder
Set ConFolder = olns.GetDefaultFolder(10)
' Get the items in the folder
Set ConItems = ConFolder.Items
' Get the total items in the Contacts folder
NumItems = ConItems.Count


' Loop through the items in the Contacts folder,
' filling the array with data and keeping track
' of the number of contacts found.

NumContacts = 0
For I = 1 to NumItems
Set itm = ConItems(I)
Control.AddItem itm.FileAs
Next

End Sub

Again, this gives me an incomplete and out of order drop down list....
Any thoughts
 
S

Sue Mosher [MVP-Outlook]

You can use the Items.Sort method to sort on the desired field before you
loop through all the items.
 
D

Dave Hampton

Thanks Sue,

I revamped my code (Based on some stuff in the microsoft support area)
It works great - except - I'm only getting a portion of the contacts.
Is their a limit to the # of list elements I can have?

If so, perhaps their is another way around the problem...
The purpose of the custom form is to track a property address and loan
associated with it.

My contact list has everyone I know. From that list, a person might be
a Buyer, Seller, Realtor or a combination on a particular property
address. They may also appear on multiple items in the folder
(different properties - like a realtor that assists on two different
properties...)

I want to be able to open a property file and see contact links in each
of my defined fields (seller, buyer, agent, etc.)

Later, I want to be able to open up a contact and see what properties
they are associated with under the activities tab and in what capacity
(seller, buyer, agent etc.)

My first step is just to be able to input contacts only in each of the
user defined fields. - I need to make it simple to input (like a contact
selector or combolist/list).

Step two is to make it tie into the activities for the contacts in the
contacts folder.

Step three is to make separate activities that tie them in a s Realtor,
Buyer, Seller, Etc. instead of just into the property.

Any help with step one would be great.

thanks


Sub Item_Open()

Set objFormPage = Item.GetInspector.ModifiedFormPages("General")
Set objListBox = objFormPage.Controls("ComboBox1")

Set objNS = Application.GetNamespace("MAPI")
' Get the default Contacts folder
Set objConFolder = objNS.GetDefaultFolder(10) ' olFolderContacts

' Get the items in the folder
Set colConItems = objConFolder.Items

' Create a Restrict filter
strFilter = "[FileAs] <> " & Chr(34) & Chr(34)

' Just get those contacts with a Full Name
Set colResItems = colConItems.Restrict(strFilter)

' Sort the contacts based on FullName
colResItems.Sort "[FileAs]"

' Loop through all of the sorted contacts
For Each objItem in colResItems
' Add the name to the listbox
objListBox.AddItem objItem.FileAs
Next
' Loop through all of the sorted contacts
For Each objItem in colResItems
' Add the name to the listbox and ignore distribution lists
If Left(objItem.MessageClass, 11) = "IPM.Contact" Then
objListBox.AddItem objItem.FileAs
End If
Next

End Sub


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
S

Sue Mosher [MVP-Outlook]

Outlook may run out of memory if you have hundreds of contacts in the
folder. See http://www.outlookcode.com/d/code/popcombobox.htm for an
alternative approach using CDO.

I don't understand why you're looping through the list twice.

For ease of opening the related contact record, you may want to use a
multi-column combo box with a hidden second column holding the contact's
EntryID. If you do that, you can use the Namespace.GetItemFromID method to
open the contact. To fill a multi-column list or combo box, you'll usually
want to use an array to set the value of control.List, rather than using
AddItem for each row.


--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Dave Hampton said:
Thanks Sue,

I revamped my code (Based on some stuff in the microsoft support area)
It works great - except - I'm only getting a portion of the contacts.
Is their a limit to the # of list elements I can have?

If so, perhaps their is another way around the problem...
The purpose of the custom form is to track a property address and loan
associated with it.

My contact list has everyone I know. From that list, a person might be
a Buyer, Seller, Realtor or a combination on a particular property
address. They may also appear on multiple items in the folder
(different properties - like a realtor that assists on two different
properties...)

I want to be able to open a property file and see contact links in each
of my defined fields (seller, buyer, agent, etc.)

Later, I want to be able to open up a contact and see what properties
they are associated with under the activities tab and in what capacity
(seller, buyer, agent etc.)

My first step is just to be able to input contacts only in each of the
user defined fields. - I need to make it simple to input (like a contact
selector or combolist/list).

Step two is to make it tie into the activities for the contacts in the
contacts folder.

Step three is to make separate activities that tie them in a s Realtor,
Buyer, Seller, Etc. instead of just into the property.

Any help with step one would be great.

thanks


Sub Item_Open()

Set objFormPage = Item.GetInspector.ModifiedFormPages("General")
Set objListBox = objFormPage.Controls("ComboBox1")

Set objNS = Application.GetNamespace("MAPI")
' Get the default Contacts folder
Set objConFolder = objNS.GetDefaultFolder(10) ' olFolderContacts

' Get the items in the folder
Set colConItems = objConFolder.Items

' Create a Restrict filter
strFilter = "[FileAs] <> " & Chr(34) & Chr(34)

' Just get those contacts with a Full Name
Set colResItems = colConItems.Restrict(strFilter)

' Sort the contacts based on FullName
colResItems.Sort "[FileAs]"

' Loop through all of the sorted contacts
For Each objItem in colResItems
' Add the name to the listbox
objListBox.AddItem objItem.FileAs
Next
' Loop through all of the sorted contacts
For Each objItem in colResItems
' Add the name to the listbox and ignore distribution lists
If Left(objItem.MessageClass, 11) = "IPM.Contact" Then
objListBox.AddItem objItem.FileAs
End If
Next

End Sub


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 

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