List box selection returns autotext entries

P

PiaD

I am having trouble getting a multiselect list box to match up with autotext
entries. For instance, I would like a user to select which autotext entries
they want to include in a document. I was hoping they can select the items
from a multi select list box on a user form. Based on their selections, the
corresponding autotext entry will populate in the document. How do I make an
autotext entry correspond to a list box item? Also - can I control where the
autotext is inserted with bookmarks????

Thank you,
Pia
 
C

Cindy M.

Hi Pia,
I am having trouble getting a multiselect list box to match up with autotext
entries. For instance, I would like a user to select which autotext entries
they want to include in a document. I was hoping they can select the items
from a multi select list box on a user form. Based on their selections, the
corresponding autotext entry will populate in the document. How do I make an
autotext entry correspond to a list box item? Also - can I control where the
autotext is inserted with bookmarks????
Which version of Word is involved, here?

You need to give us more information about the environment you're working in...

What kind of "identifier" have you been trying to use to match the selections
with the AutoText entries, and what kind of problems have resulted?

Are you pulling the AutoText entries from a single template, or more than one
template (in other words, could there be problems with templates getting mixed
up)?

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
P

PiaD

Hi Cindy,
I am using Word 2003. My code looks like this:

Private Sub DisplayButton2_Click()
Dim rng As Range
Dim idx As Long

Set rng = ActiveDocument.Range
rng.Collapse wdCollapseEnd
With ListBox1
For idx = 0 To .ListCount - 1
If .Selected(idx) Then
ActiveDocument.AttachedTemplate _
.AutoTextEntries("Overview" & idx).Insert Where:=rng
Set rng = ActiveDocument.Range
rng.Collapse wdCollapseEnd
End If
Next
End With

Me.Hide
End Sub


Private Sub UserForm_Initialize()
Me.ListBox1.AddItem "1. View Plan", Index = 1
Me.ListBox1.AddItem "2. View Results", Index = 2
etc...about 10 autotext entries....
End Sub

I get an error 5441 Cannot find member selection. I don't know how to
properly identify the autotext and link it to the checkbox. All Autotext is
in the same template I have named them all "Overview1" "Overview2" etc.

Any guidance is greatly appreciated!!!!
 
J

Jay Freedman

Hi Pia,

The problem is a one-off error. The indexes of the listbox entries run from 0 to
..ListCount - 1, but the names of the AutoTextEntries run from 1 to .ListCount.
You need to change the .Insert statement to

.AutoTextEntries("Overview" & (idx + 1)).Insert

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
P

PiaD

Hi -
That seems to do the trick. Thank you! I am hoping you can tell me how I can
preserve the formatting in my autotext entries (which include H1's and H2's
and other custom styles). It appears that I lose the style formats of the
AutoText entries and all the styles default to the style where my cursor is
set when the Userform pops up. When I go to insert the autotext manually,
the styles work great. Do you know why the styles get lost when applied
through the Userform?

Regards,
Pia
 
J

Jay Freedman

Hi Pia,

The .Insert method for an AutoText entry takes an optional boolean parameter,
RichText. When this parameter is set to True, the original formatting of the
entry is retained; but the default value (used when you omit the parameter) is
False. So make your statement

ActiveDocument.AttachedTemplate _
.AutoTextEntries("Overview" & (idx + 1)).Insert Where:=rng, RichText:=True
 
P

PiaD

Awesome! Thank you. Works like a charm!

Jay Freedman said:
Hi Pia,

The .Insert method for an AutoText entry takes an optional boolean parameter,
RichText. When this parameter is set to True, the original formatting of the
entry is retained; but the default value (used when you omit the parameter) is
False. So make your statement

ActiveDocument.AttachedTemplate _
.AutoTextEntries("Overview" & (idx + 1)).Insert Where:=rng, RichText:=True
 

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