Leu,
The basic process is as follows:
Create an AutoNew macro that runs when you “launch†the template to create a
new document from it. This macro sets up some variables to store the values
that go into your userform and one additional variable to store the “statusâ€
of the document; i.e. is it a new document or one that’s been created
previously and you’re now opening for editing?
Then in the code that actually creates the document (by clicking OK in the
userform or whatever other method you use) store the values from the userform
in the variables you set up when you first launched the template.
Then create a “rerun†macro that just loads and redisplays the original
userform. The initialisation code in the userform checks the “statusâ€
variable and if it’s a document that’s been created previously, loads the
variable information stored in the document into the appropriate controls on
the userform.
In the example below I have a userform called “frmOtherDetails†that has one
textbox control on it called “txtDocTitle†and a command button called
“btnOK†(as well as a ‘Cancel’ button). In addition to the userform I have an
AutoNew module and a module called “RerunOtherâ€. And in the body of the
template itself, I have a bookmark called “txtDocTitle†for inserting the
value from the textbox into the document. You could use a formfield or some
other method, but if you use a bookmark, it’s important that you understand
how this works.
This should be enough for you to figure out what I’m doing.
NOTE: A good portion of the code in the "real" template has been omitted
from the following for clarity. The code that’s reproduced here just shows
how to save the values from the userform and reload the values back into the
userform when you rerun the template.
CODE IN THE ‘AutoNew’ MODULE
Sub AutoNew()
‘*** Create the necessary variables and then load and show the userform ***
CreateDocVars
Load frmOtherDetails
frmOtherDetails.Show
End Sub
Sub CreateDocVars()
‘*** Create two variables: one for storing the “status†of the document and
one for the value that will be entered in the UserForm; you can create as
many as you need for storing the values from your userform. ***
With ActiveDocument.Variables
.Add ("DocNew")
.Add ("DocTitle")
End With
End Sub
CODE BEHIND THE ‘frmOtherDetails’ USERFORM
Private Sub UserForm_Initialize() ‘ Initialise the userform
‘*** If the ‘DocNew’ variable has a value of “False†(i.e. this is an
existing document that’s being rerun, not a new document just being created)
then load the variables. ***
If ActiveDocument.Variables("DocNew").Value = "False" Then LoadDocVars
End Sub
Private Sub LoadDocVars()
‘*** Load the value from the ‘DocTitle’ variable into the ‘txtDocTitle’
textbox on the userform; this only happens if it’s not a new document. ***
txtDocTitle.Value = ActiveDocument.Variables("DocTitle").Value
‘*** Also because ‘txtDocTitle’ is a required field (in my template) I don’t
have to check to make sure there’s a value in the variable. However, if it
was an optional field I’d probably modify this a bit like this: ***
‘ If ActiveDocument.Variables("DocTitle").Value <> “ †Then
txtDocTitle.Value = ActiveDocument.Variables("DocTitle").Value Else Then
txtDocTitle.Value = “â€
‘*** Otherwise you get a single space loaded into the textbox, which is the
value that gets saved in the variable if there’s no value in the textbox
originally. ***
End Sub
Private Sub btnOK_Click()
‘*** When you click the ‘OK’ button, hide the userform, save the values from
the userform into the variables, insert the values from the userform into the
document and then unload the userform. ***
frmOtherDetails.Hide
SaveDocVars
InsertValues
Unload frmOtherDetails
End Sub
Private Sub SaveDocVars()
‘*** Save the values from the userform into the variables: ***
With ActiveDocument
‘*** the value from the ‘txtDocTitle’ textbox into the ‘DocTitle’ variable;
***
.Variables("DocTitle").Value = txtDocTitle.Value
‘*** and a value of “False†into the ‘DocNew’ variable to indicate that it’s
not a new document anymore. ***
.Variables("DocNew").Value = "False"
End With
End Sub
Private Sub InsertValues()
‘*** Insert the value from the ‘txtDocTitle’ textbox into the document by
first selecting the bookmark (not pretty, I know, but necessary – I’ll
explain why)... ***
ActiveDocument.Bookmarks("DocTitle").Select
With Selection
‘*** then set the text of the selection to the value from the textbox ***
.Text = txtDocTitle.Value
‘*** then reinsert the ‘DocTitle’ bookmark around the selection (this is the
reason you have to select the bookmark to begin with). ***
.Bookmarks.Add "DocTitle", .Range
End With
End Sub
CODE IN THE ‘RerunOther’ MODULE
Public Sub RerunOther()
‘*** Just load and show the userform; the code in the userform
initialisation subroutine does the heavy lifting. ***
Load frmOtherDetails
frmOtherDetails.Show
End Sub
Conclusion
This is a _really_ simple example. Some of the others templates I use are
much more complicated. For example, I build lists of values for the attendees
of a meeting and need to save these into variables so the document can be
rerun. In this case, I create the variables dynamically when I create the
document. I also delete them and recreate them dynamically when I rerun the
document. And the code for inserting the values into the document is also
more complex; however, it’s still the same concept: select the requisite
bookmark, insert the text into it and reapply the bookmark.
Hope this answers your question!