Insert AutoText entries based on selected options in UsrForm cbobo

N

NettysGirl_mi

Word 2003 on Windows XP. My template will be used to create design documents
with up to 12 different sections ("sections" meaning blocks of text, no
Section Breaks involved). Examples of sections are Requirements,
Assumptions, Calculations, Examples, etc. I have defined AutoText entries
for each of the variations that can occur for each of the sections. For
example, there are AutoText entries for RequirementsA, RequirementsB,
AssumptionsA, etc. Thanks to one of the MVP websites, I now have a userform
with 12 comboboxes that list out all of the AutoText entries, so users can
select which sections/variations they want included in the document, in
order. Here is the coding for that (which works great):

-------------------------------------
Sub showSelectSectionsForm()

Dim i As Long,
Dim UFrm As form_SelectSections
Dim arrAutoText() As String
Set UFrm = form_SelectSections

ReDim
arrAutoText(ActiveDocument.AttachedTemplate.AutoTextEntries.Count - 1)
For i = 0 To ActiveDocument.AttachedTemplate.AutoTextEntries.Count - 1
arrAutoText(i) = ActiveDocument.AttachedTemplate.AutoTextEntries(i +
1).Name
Next i
WordBasic.SortArray arrAutoText()
UFrm.cbx_Selection01.List = arrAutoText()
UFrm.cbx_Selection02.List = arrAutoText()
……
UFrm.cbx_Selection12.List = arrAutoText()

With UFrm
.Show vbModeless
End With

End Sub
-----------------------------------------

Now I need to write up my OK button click event, and I can't figure it out.

Here is a macro that I recorded that shows what I am trying to accomplish
(kind of –the recorder is limited of course).

The first AutoText will always be inserted at Bookmark "Insert_Sections".
All AutoTexts in this template end with a bookmark called "Insert_Next", so
each subsequent AutoText will be inserted after the previous one (and the
previous "Insert_Next" bookmark is replaced by one at the end of this new
AutoText).

----------------------------------
Selection.GoTo What:=wdGoToBookmark, Name:="Insert_Sections"
ActiveDocument.AttachedTemplate.AutoTextEntries( _
"1 Requirements ").Insert Where:=Selection.Range, RichText:= _
True
Selection.GoTo What:=wdGoToBookmark, Name:="Insert_Next"
Selection.TypeParagraph
ActiveDocument.AttachedTemplate.AutoTextEntries( _
"4 Assumptions-Source ").Insert Where:=Selection.Range, _
RichText:=True
Selection.GoTo What:=wdGoToBookmark, Name:="Insert_Next"
Selection.TypeParagraph
ActiveDocument.AttachedTemplate.AutoTextEntries( _
"5 Assumptions-Destination").Insert Where:=Selection.Range, _
RichText:=True
----------------------------------

I can't figure out how to replace the literal name of the AutoText entry
(such as "1 Requirements") with the option selected in cbx_Selection01, then
the next one ("4 Assumptions-Source") with the option selected in
cbx_Selection02, etc. I know how to return the index value, but that just
tells me where in the list the selection falls. I don't know how I would use
that to determine the AutoText name.

Also, it seems like there should be a way to loop through this without
having to specify each individual combobox name, but again, I'm not sure how
to replace the literal with a variable to use in a for/next loop.

Any suggestions are much appreciated.
 
F

Fumei2 via OfficeKB.com

YOU have to work out the logic. YOU have to make the decisions.

One way to make it easier (but I am not sure it will work for you in this
case) is to match item names in the combobox with the AutoText names.

What I am not following is this: "I now have a userform with 12 comboboxes "

Is this literally true? You have 12 comboboxes, and each combobox (I assume)
has multiple items? It sort of looks lik eeach comboboxc gets filled with
the SAME items.

UFrm.cbx_Selection01.List = arrAutoText()
UFrm.cbx_Selection02.List = arrAutoText()

If this is the case, I have to ask why?

In any case, you already have an array of the AutoText, so match each item in
the array to an item in the combobox.
 
N

NettysGirl_mi

(I'm going to call my initial bookmark "Insert_Next" like the others, so the
first insert can be handled just like the others.)
 
N

NettysGirl_mi

Thanks for your response. There are 12 comboboxes populated with the same
list items because the user has 12 opportunities to select items from the
same list. I don't want to use a single multi-select listbox because that
won't tell me what order they want the items entered into the document. With
a multi-select listbox, they might select the first item and second item, but
want the second item inserted as Section1 and the first item inserted as
Section2. The easiest way I could think of to keep track of which entry is
inserted where in the doc is to use separate listboxes with labels saying
"Pick the text for Section 1, pick the text for Section 2" etc. That way,
whatever is selected in cbx_Selection01 is inserted first, whatever is
selected in cbx_Selection02 is inserted second, etc, regardless of where the
value falls in the list.

I've seen some code for "diminishing" lists, where multiple boxes are
populated with the same list and once an item is selected from one list, it
will not show up again in subsequent lists. That looked cool but really
isn't required for this project. I'll trust my users to know not to select
the same entry in two different boxes.

You suggest "match each item in the array to an item in the combobox." That
is what I am asking how to do.

Thanks again.
 
N

NettysGirl_mi

In case anyone finds this while searching for a similar question, my answer
turned out to be simple: Just return the combobox Value property rather than
IndexValue. I know my code below is rudimentary -- there's got to be a way
to loop through instead of all those If Thens, but it works and I'm pressed
for time so I'm going with it:

Section1 = form_SelectSections.cbx_Selection01.Value
Section2 = form_SelectSections.cbx_Selection02.Value
....
If Section1 <> "" Then
Selection.GoTo What:=wdGoToBookmark, Name:="Insert_Next"
Selection.TypeParagraph
ActiveDocument.AttachedTemplate.AutoTextEntries(Section1).Insert
_Where:=Selection.Range, RichText:=True
Else
GoTo Finished
End If

If Section2 <> "" Then
Selection.GoTo What:=wdGoToBookmark, Name:="Insert_Next"
Selection.TypeParagraph
ActiveDocument.AttachedTemplate.AutoTextEntries(Section2).Insert
_Where:=Selection.Range, RichText:=True
Else
GoTo Finished
End If
.....
 

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