User Forms

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have created a User Form that runs when the document opens that propmts for
data to be entered. This data is then attributed to bookmarks within the
document.
I have created a macro that recalls the User Form so that I can change data
is necessary, however when the User Form is recalled it is blank and does not
recognise the previously inserted information.

How do I fix this?
 
In the Userform_Activate() procedure of the userform, you must create
code that gets the values of the bookmarks and puts those values into
the controls of the userform. This procedure runs just before the
userform appears on the screen.

For example, if you have a bookmark named "Address" in the document,
and a corresponding text box control named "tbxAddress" in the
userform, the Userform_Activate() procedure should include the line

tbxAddress.Text = ActiveDocument.Bookmarks("Address").Range.Text

You can use similar code to initialize check boxes, list boxes, and
other controls in the userform from data in the document.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
I would suggest that you use DocVariable fields instead of bookmarks as all
you need to do then is load the controls on the form with the values in the
document variables and replacing the data in the document is easier with
DocVariables rather than bookmarks as you will probably need code to
re-create the bookmark if you change the data that is in it.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
Here's the code from a userform that uses that approach to enter the
information into the document and to load it back into the userform if the
form is run once again

Private Sub CommandButton1_Click()
With ActiveDocument
If txtCompany = "" Then
.Variables("Company").Value = " "
Else
.Variables("Company").Value = txtCompany
End If
If txtProject = "" Then
.Variables("Project").Value = " "
Else
.Variables("Project").Value = txtProject
End If
If txtDocNum = "" Then
.Variables("DocNum").Value = " "
Else
.Variables("DocNum").Value = txtDocNum
End If
If txtRevNum = "" Then
.Variables("Revision").Value = " "
Else
.Variables("Revision").Value = txtRevNum
End If
If txtDocTitle = "" Then
.Variables("DocTitle").Value = " "
Else
.Variables("DocTitle").Value = txtDocTitle
End If
.Fields.Update
.Sections(1).Headers(wdHeaderFooterPrimary).Range.Fields.Update
.Bookmarks("Text").Range.Select
End With
Unload Me
End Sub

Private Sub UserForm_Initialize()
With ActiveDocument
txtCompany = .Variables("Company").Value
txtProject = .Variables("Project").Value
txtDocNum = .Variables("DocNum").Value
txtRevNum = .Variables("Revision").Value
txtDocTitle = .Variables("DocTitle").Value
End With
txtDocNum.SetFocus
End Sub


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
Are you sure that the names of the controls on the form are identical to
what the code is expecting:

For example,

..Variables("Company").Value = txtComp

is there a control named txtComp?

In the code, if you insert a period after txtComp, the intellisense should
give you a list of attributes to choose. The one that you want is the .Text
one which is the default.

If the list doesn't appear, then the name used in the code is not the name
of a control on your form.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
BTW, the reason for using the If...then...Else... construction is to load
the variable with a space if the user does not enter anything into the
control on the user form. Without that, the variable would not be created
and then an error would occur if there was an attempt to get the values from
the variables to appear in the userform.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
Back
Top