PDA

View Full Version : inserting autotext through userform



kxw1
07-27-2007, 12:35 PM
I am very new to userforms...have just figured out how to use comboboxes & textboxes. I was wondering if there is an easy way to insert autotext entries using a userform with either a checkbox or combobox?

fumei
07-27-2007, 01:31 PM
You can insert Autotext by code, if that is what you are asking.

Say you have the following AutoText entries:
Buddy
Charlie
Frank

Private Sub UserForm_Initialize()
Dim Stuff()
Dim var
Stuff = Array("Buddy", "Charlie", "Frank")
For var = 0 To UBound(Stuff)
ComboBox1.AddItem Stuff(var)
Next
End Sub

Sub CommandButton1_Click()
ActiveDocument.AttachedTemplate.AutoTextEntries(ComboBox1.Text).Insert _
Where:=Selection.Range, RichText:=True
Unload Me
End Sub

These are loaded into the combobox from an array with the userform Initialize event.

The commandbutton takes the text value of the combobox, and passes it as the Autotext entry name, which is inserted (in this case) at the Selection point.

kxw1
07-30-2007, 07:19 AM
Great, thank you! How would I insert the autotext at a bookmark?

fumei
07-30-2007, 12:06 PM
Autotext is inserted where ever you want it. Normal (common) usage is at the selection, but it can be anywhere. Notice the bold in the code below. ActiveDocument.AttachedTemplate.AutoTextEntries(ComboBox1.Text).Insert _
Where:=Selection.Range, RichText:=True What does that mean?

It means the AutoText has a Where do you want this to go parameter. Which makes sense. Like most (if not all) "where" questions in Word, this means a Range.

Again, the most common range to use is the Selection.Range, ie. where the cursor is. However, it most certainly does not have to be. ANY valid range can be used.

So, you have a bookmark? Bookmarks are simply named ranges. Use it. The long writing way: ActiveDocument.AttachedTemplate.AutoTextEntries(ComboBox1.Text).Insert _
Where:=ActiveDocument.Bookmarks("whatever").Range, _
RichText:=True , or perhaps a tidier way:
Dim r As Range
Set r = ActiveDocument.Bookmarks("whatever").Range
ActiveDocument.AttachedTemplate.AutoTextEntries(ComboBox1.Text).Insert _
Where:=r, RichText:=True In either case, the "Where" is a Range.

kxw1
08-01-2007, 09:43 AM
I had been using the following code to insert text from a texbox to a bookmark in the same template, but I can't figure out how to get that to work with the code above.


Private Sub CommandButton1_Click()
With ActiveDocument
.Bookmarks("FIRSTNAME").Range _
.InsertBefore TextBox1
.Bookmarks("LASTNAME").Range _
.InsertBefore TextBox2
End With
UserForm1.Hide
End Sub


Thank you so much for your help. I have been struggling over this for weeks!

fumei
08-01-2007, 10:36 AM
This has nothing to do with your thread subject - inserting AutoText. This has been answered.

I can't figure out how to get that to work with the code above.
Huh? What are you asking??? Why would it work with the "code above"? The code above was to insert AutoText. The code you just posted has nothing to do with, and does not use, AutoText.

BTW: your code puts the values BEFORE the bookmark, not IN the bookmark.

Oh, and why are you using .Hide?