Repopulate UserForm from Document

B

Barb

I have created a 2003 Word Template [2007 Compatibility Mode] that uses 2
userforms. (The 2 userforms are identical, except for the Name and some of
the VBA coding.)

The first userform loads when the template opens as a document to which the
user enters information in each field and upon clicking OK, the new document
populates with the entered information.

The second userform was created to appear with the help of a keyboard
shortcut macro to appear so that the user has the option of changing the
previously entered information.

My problem is that I want the second userform to populate with the
information that was previously entered. Here is a sampling of my code for
the second userform:

Private Sub DisplayEditForm1()
With ActiveDocument
txtProjectDescription =
ActiveDocument.Variables("ProjectDescription").Value
etc…..
End With
End Sub

I have tried some of the other posted suggestions, but I'm still having
problems.

Does anyone have any suggestions?

Thanks bunches!!
 
J

Jay Freedman

I'll assume that the first userform puts values into document variables, and
that the document displays those values in DocVariable fields, so the named
variables actually have values when the second userform launches.

The code for launching the second userform should look something like this:

Private Sub DisplayEditForm1()
Dim dlg As UserForm2 ' or whatever you named the second userform
Set dlg = New UserForm2

With ActiveDocument
dlg.txtProjectDescription = .Variables("ProjectDescription").Value
' etc.
End With

dlg.Show

Set dlg = Nothing
End Sub

Notice that txtProjectDescription is a text field in the userform, so it's a
member of dlg, indicated be the "dlg." in front of it. Do the same with the
other text fields in the userform.

Similarly, the .Variables collection is a member of the ActiveDocument object;
in this case, the ActiveDocument in front of .Variables is implied by the With
statement.

The dlg.Show is what makes the userform appear on screen. When the userform
hides itself (using Me.Hide in the code of the OK button), execution returns to
the next line in the macro, setting the dlg object to Nothing which frees the
userform's memory.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
G

Gordon Bentley-Mix

Barb,

Jay's post gives a very good explanation of how to achieve your desired
outcome. You may also find the information in my posts under the thread "Save
userform data" helpful.

However, I question the need to have a second UserForm that is identical to
the first with only some variation in the code (and the name - which becomes
a moot point). I frequently provide this sort of "rerun" functionality using
only one UserForm. I do this by using a document variable to track whether
I'm creating a new document or rerunning an existing one. The code I posted
in the thread referenced above shows how to do this.
--
Cheers!
Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
B

Barb

Jay and Gordon, thanks so much for your input; you’re both amazing!

Since I’m relatively a newbie at VBA and this is a lot of information for me
to digest, I’m going to work with the coding you both suggested to see which
works best for my application.

Thanks again!!
 

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