Userforms and use of form fields

J

JohnS

I have an automatic document template for Microsoft Word using VBA to
display a User Form into which the user enters information. The
standard document has bookmarks to get the right info at the right
place.

With ActiveDocument
.Bookmarks ("Name").Range.Text = txtName.Value
.Bookmarks ("Adres").Range.Text = txtAdres.Value
etc.
End With

What I want is to use Form fields instead of bookmarks to get the
information from the user form transferred to the right form field. Is
it possible to place the bookmark IN the field? Anyone can help me
with the vba code to get this working?

Thanks!
 
J

Jezebel

Why use Bookmarks at all?. Document properties and DocProperty fields are
much easier to manage.

With ActiveDocument
.CustomDocumentProperty("Name") = txtName.Value
.CustomDocumentProperty ("Adres") = txtAdres.Value
:
.Fields.Update
End With

In the body of your document: { DocProperty Name } ... { DocProperty Adres }
 
B

Bill Foley

If you want to use a Userform to fill in a formfield text box, consider
this:

1. Each Formfield you create has a bookmark property (Text1, for example).
This can be entered by double-clicking the text formfield itself.
2. Your UserForm has textboxes in which to type information. You obviously
name them using the "Properties Window" with the object selected.
3. Code behind a button that takes the info in your textboxes and placed
them in your formfields might be:

Private Sub FillInData_Click()
ActiveDocument.FormFields("Text1").Result = TextBox1
ActiveDocument.FormFields("Text2").Result = TextBox2
Unload UserForm
End Sub

If you want the fields updated when you close the UserForm, something must
be done to update them. One option is to go to PrintPreview. then back. If
you did that (I put it before the "Unload UserForm" line), the code would
be:

ActiveDocument.PrintPreview
ActiveDocument.ClosePrintPreview

You could also use the method designed for this instead:

ActiveDocument.Fields.Update

If you want to unlink the formfields after the data has been entered, you
could use:

ActiveDocument.Fields.Unlink

Hope this helps!
 

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