Hi Tony,
What you really need to do is create a template from which you create the
documents
in which you want this facility.
This example shows how you would deal with two pieces of information
that you want to put into the document. By adding more of those steps, you
can
deal with more variables
1. Create two bookmarks in your template named "Text1" and "Text2" (add
additional bookmarks for each piece of data)
2. Go to the Visual Basic Editor (Tools>Macro>Visual Basic Editor)
3. With the Template selected in the Project Explorer, Insert a UserForm
(I am assuming that it will be UserForm1)
4. In the UserForm, insert 2 text boxes - assumed to be TextBox1 and
TextBox2
5. Dimension them as required for the text that you intend to be inserted
into them
6. Add any labels that you want to have on the form to assist the user
7. In the UserForm, insert a CommandButton - assumed to be CommandButton1
8. Right Click on the CommandButton and click on View Code
9. Enter the following code:
Private Sub CommandButton1_Click()
With ActiveDocument
.Bookmarks("Text1").Range.InsertBefore TextBox1
.Bookmarks("Text2").Range.InsertBefore TextBox2
End With
UserForm1.Hide
End Sub
(Repeat the lines With ActiveDocument.Bookmarks and .InsertBefore for each
bookmark. Similarly, you can go to a second bookmark and insert the same
piece of data.)
12. Close the Visual Basic Editor
13 From the Tools Menu select Macro, then Macros, then with the Template
selected in the "Macros in:" box, type the name autonew in the "Macro name:"
box and click the Create button.
14 Enter the following command in the module screen:
UserForm1.Show
15. Exit from the Visual Basic Editor and save your template.
When you create a new document from the template, the autonew macro opens
the user form for the user to input data, then when the command button is
clicked, the code associated with that button moves the selection to each of
the bookmarks in the document and inserts the relevant piece of data into
them.
Hope this helps,
Doug Robbins - Word MVP
Please post any follow-up to the Newsgroups so that others may benefit
therefrom or contribute thereto.
Quote:
> I Have a word document that I would like to add VB code that will have a
> user form that will prompt me for input and than put the information in
the
> right places on my word document. I tried to experiment but I can't figure
> it out , please help. If you can tell me the basics I think I can figure
the
> rest out. Thanks