insert autotext into form field

D

dragon

Hey - moving along rapidly with my project and came to another glitch. I
have 5 form fields in a document. I want the program to go to the last form
field and insert specific autotext into that form field.

Anybody have any suggestions?

Thanks!

Dragon
 
D

Doug Robbins - Word MVP

HI Dragon,

Sub Macro1()

' Macro created 15-11-97 by Doug Robbins to replace a formfield entry with
autotext
'
On Error GoTo EndSub
userid = ActiveDocument.FormFields("Text1").Result
Address = ActiveDocument.AttachedTemplate.AutoTextEntries(userid).Value
ActiveDocument.FormFields("Text1").Result = Address

EndSub:
Exit Sub

End Sub
Sub Macro2()
'
' Macro created 15-11-97 by Doug Robbins to add the address corresponding to
a drop down name
'
Set myDrop = ActiveDocument.FormFields("Dropdown1").DropDown
Company = myDrop.ListEntries(myDrop.Value).Name
Address = ActiveDocument.AttachedTemplate.AutoTextEntries(Company).Value
ActiveDocument.FormFields("Text1").Result = Address

End Sub

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
D

dragon

Thanks Doug. A couple things I need help with....

First off, I was planning on placing this into a userform in the
command_click sub. Are there any variations I need to make to it?
Second of all, I don't have the ability to give the formfield a bookmark (I
am assuming that "text1" is referring to a bookmark) because I don't have
access to the templates ahead of time. However, I do know that they will
have a set number of form fields, the last of which I want to drop the
autotext in. Is there a way to specify for the text to go into the last
form field (I think I have read some statements that refer to it as "-1")?
Third, are these two options to meet the same end or are they both necessary
to accomplish the goal? It appears that the first deals with autotext and
the second with the text as a dropdown option.

Thanks again for the help,

Dragon
 
J

Jay Freedman

Hi, dragon,

If you don't know the name of the form field but you do know that it's the
last one, and you know the name of the AutoText entry, then this ought to
work:

Private Sub CommandButton1_Click()
Dim oFld As FormField
Set oFld = ActiveDocument.FormFields( _
ActiveDocument.FormFields.Count)
oFld.Result = ActiveDocument.AttachedTemplate _
.AutoTextEntries("NameOfAutoText")
Set oFld = Nothing
Me.Hide
End Sub

Change the name of the sub according to whatever you name the command
button, and replace NameOfAutoText with the correct name. The Me.Hide
command assumes that the macro that displays the userform will also unload
it.

Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://www.mvps.org/word
 
D

Doug Robbins - Word MVP

Hi Dragon

This one:

Set oFld = ActiveDocument.FormFields(ActiveDocument.FormFields.Count)

If there are 10 formfields in the document, then
ActiveDocument.FormFields.Count will return 10, so in effect the above line
is that same as

Set oFld = ActiveDocument.FormFields(10)

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
J

JC

Doug,

so far this seems to be working to a point. It is only displaying the first
four lines of the autotext. It gets cut-off midsentence. I checked the
options of the formfield and the length is unlimited. Any thoughts?

Also, it is dropping it into the form field itself so the text is not
replacing the form field. Any thoughts how to replace the form field? This
also could be the problem with the limit mentioned above....?
 
D

dragon

I figured out the answer so you don't need to post. If anyone is interested
I will post it.
 
D

dragon

Thanks Doug.

I had seen that article, but the line that states Str1 = (a long string >
256 characters) was giving me problems.
 
D

Doug Robbins - Word MVP

Hi Dragon

Use

Str1 = ActiveDocument.AttachedTemplate.AutoTextEntries([autotext]).Value

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a consulting basis.

Hope this helps
Doug Robbins - Word MVP
 

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