How to load a combobox from a string

  • Thread starter Southern at Heart
  • Start date
S

Southern at Heart

The following code gets a list of all the different Company names used in my
Outlook folder. (maybe there's a better way of doing this...)
Now I have the string CompanyList set to a few dozen names, with a delimiter
of vbCrLf. How do I load this into my combobox on my userform, for the user
to select one? (Do I want a combobox or a listbox--I'd like the user to be
able to choose several, actually)
thanks,
Southern@Heart

Public CompanyList As String

Sub Outlook_Company()
Dim ol As New Outlook.Application
Dim olns As Outlook.Namespace
Dim cf As Outlook.MAPIFolder
Dim c As Outlook.ContactItem
Dim objItems As Outlook.Items
Set olns = ol.GetNamespace("MAPI")
Set cf = olns.GetDefaultFolder(olFolderContacts)
Set objItems = cf.Items
iNumContacts = objItems.Count
System.Cursor = wdCursorWait
If iNumContacts <> 0 Then
For I = 1 To iNumContacts
If TypeName(objItems(I)) = "ContactItem" Then
Set c = objItems(I)
If c.CompanyName <> "" Then AddCompany (c.CompanyName)
'strCompany = c.CompanyName
End If
Next I
End If
System.Cursor = wdCursorNormal
MsgBox CompanyList
End Sub
Sub AddCompany(strCompany As String)
If CompanyList = "" Then
CompanyList = strCompany
Exit Sub
End If
If Not InStr(1, CompanyList, strCompany) Then
CompanyList = CompanyList & vbCrLf & strCompany
End If
End Sub
 
D

Doug Robbins - Word MVP

Use the Split() function to covert the string to an Array and then use

[comboboxname].List() = [ArrayName]

to load the list into the combobox. Replace the squarebrackets and the text
within in them by the name of the combobox control and the name of the
array.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
S

Southern at Heart

I can't quite figure out how to do this. I've tried the code below but it
doesn't work. Can someone please help me use this split function to change
CompanList which is a vbCrLf seperated list into an Array?
Thanks.
Don't you have to know how big your arrary is before you set it in the Dim
statement? But I won't know how big it needs to be till after I use the
split function.
confused,
Southern@Heart


dim List as Variant

List = Array(Split(CompanyList, vbCrLf))
Debug.Print List(1)
Debug.Print List(20)
Me.ComboBox1.List = List
 
H

Helmut Weber

Hi,

Sub Test6666a()
Dim List() As String

List = Split("1,2,3,4,5", ",")
Debug.Print List(1)
Debug.Print List(20) ' out of range of course
End Sub

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 

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