Filling Combo Boxes with AutoText Entries

A

Anne P.

Hi,

I have the following code in UserForm_Initialize to fill several combo boxes
with AutoText entries. I want to set a certain entry to be the default
after the items are loaded, but I can't seem to figure out how to set the
ListIndex. Short of hard coding, how do I set it to say the 4th entry in
the list? Or should I just set it based on the text that I want to appear
as the default?

Private Sub UserForm_Initialize()

'Fill the combo boxes for Greetings, Closings, Delivery and Enclosures
Dim objAtE As AutoTextEntry
Dim objTemplate As Template
'Loop through all the templates until you find the correct one
'When you do store it as objTemplate
For Each objTemplate In Templates
If objTemplate.Name = "SKGlobal.dot" Then
Exit For
End If
Next objTemplate


For Each objAtE In objTemplate.AutoTextEntries
If objAtE.StyleName = "Closing" Then
cmbClosing.AddItem objAtE.Name
End If

If objAtE.StyleName = "Salutation" Then
cmbGreeting.AddItem objAtE.Name
ListIndex = 1
End If

If objAtE.StyleName = "Mailing Instructions" Then
cmbDelivery.AddItem objAtE.Name
End If


If objAtE.StyleName = "Special Instructions" Then
cmbEnclosure.AddItem objAtE.Name
End If
Next objAtE

End Sub

Thanks in advance for any suggestions.
Anne P.
 
J

Jean-Guy Marcil

Anne P. was telling us:
Anne P. nous racontait que :
Hi,

I have the following code in UserForm_Initialize to fill several
combo boxes with AutoText entries. I want to set a certain entry to
be the default after the items are loaded, but I can't seem to figure
out how to set the ListIndex. Short of hard coding, how do I set it
to say the 4th entry in the list? Or should I just set it based on
the text that I want to appear as the default?

ListIndex works as follows (By the way, this is all in the online help file,
type ListIndex in the VBA editor window, select it and hit F1):
With a combobox containing n elements
-1 = no selection
0 = first element
n-1 = last element

So, for example, to set your combobox to the fourth element:

If objAtE.StyleName = "Salutation" Then
cmbGreeting.AddItem objAtE.Name
End If
cmbGreeting.ListIndex = 3

Of course, you have to check and make sure you have at least 4 elements in
the combobox...

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

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