I am a bit lost on how to do this...
Basically I want to have a custom userform that allows the user to enter key
pieces of information that will be used in the body of a document template.
The user will open the form from a macro and enter their info and on close
it will update the document. If they open the userform again it should load
the current set values and allow them to change if desired.
Some of the document fields that will be updated exists multiple times in
the document so each should be updated to the same entered value.
What is the best way to do this? I have tried bookmarks but they seem to
only exists once, i.e. can't reference the same item twice in a document and
also I cannot read their value on form load.
I'd like somehow to have fields that I can reference multiple times and read
and update via the userform.
Any ideas would be greatly appreciated.
You can use either custom document properties or document variables to
hold the user's values. The main difference between them is that
custom document properties can be set in the File > Properties dialog,
while document variables can be set only through VBA. A less important
difference is that doc properties can be created with different data
types (string, integer, boolean) while doc variables are always
strings.
Once you choose your weapon, insert either {DocProperty} or
{DocVariable} fields in the body of the template, referring to the
properties or variables. You can have more than one field referring to
the same property or variable, and those fields will all show the same
value. (If you insert the fields before the props/vars exist, they'll
display an error message. You do need to create the props/vars in the
template before you distribute it.)
Write your userform so that the cmdOK_Click() procedure first
validates all entries on the userform and then places the values from
the controls (textboxes, checkboxes, comboboxes, etc) into the
corresponding doc properties or doc variables. The last thing is to
update all fields in the document, so they'll show the new values.
In the Userform_Initialize() or Userform_Activate() procedure, read
the values of the doc props/vars and put those values (if they exist)
into the controls on the userform. That way, a document that's being
re-edited will initialize the controls to the previously entered
values.
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
>I am a bit lost on how to do this...
>
[quoted text clipped - 15 lines]
>
>Any ideas would be greatly appreciated.
ML - 29 Jan 2005 19:09 GMT
Do the Docvariables persist from session to session? For example if the
user opens a saved document can they be read back in for display on the
userform?
Do you have any samples of using DocVariables in this way?
> You can use either custom document properties or document variables to
> hold the user's values. The main difference between them is that
[quoted text clipped - 53 lines]
>>
>>Any ideas would be greatly appreciated.
Jay Freedman - 29 Jan 2005 22:27 GMT
Yes, document variables are saved in the file, and their values are
available after saving, closing, and reopening the document.
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
>Do the Docvariables persist from session to session? For example if the
>user opens a saved document can they be read back in for display on the
[quoted text clipped - 59 lines]
>>>
>>>Any ideas would be greatly appreciated.
ML - 29 Jan 2005 19:10 GMT
One other question, howdo you reference the document properties in the VBA
code?
Do you have a sample code snippet?
> You can use either custom document properties or document variables to
> hold the user's values. The main difference between them is that
[quoted text clipped - 53 lines]
>>
>>Any ideas would be greatly appreciated.
Jay Freedman - 29 Jan 2005 22:47 GMT
Let's assume you define document properties in the template, name them
Name1, Name2, and Street, create each one as Text (which corresponds
to the VBA data type of String), and assign each one a space character
as its value (Word won't let you assign an empty value). In the body
of the template you add fields like {DocProperty Name1} in various
places.
In the userform, you create textboxes for the values, naming them
txtName1, txtName2, and txtStreet. You want these textboxes to show
the values of the corresponding document properties. There's a small
problem: you'll get an error message if a property happens to have
been deleted (which the user could do through the File > Properties
dialog). The solution is to look through all the existing members of
the ActiveDocument.CustomDocumentProperties collection, and grab the
values from them. Any property that's been deleted simply won't be
addressed. In the Userform_Initialize() procedure, you can use code
like this:
Private Sub UserForm_Initialize()
Dim DocProp As DocumentProperty
For Each DocProp In ActiveDocument.CustomDocumentProperties
Select Case LCase(DocProp.Name)
Case "name1"
txtName1.Text = DocProp.Value
Case "name2"
txtName2.Text = DocProp.Value
Case "street"
txtStreet.Text = DocProp.Value
End Select
Next DocProp
End Sub
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
>One other question, howdo you reference the document properties in the VBA
>code?
[quoted text clipped - 57 lines]
>>>
>>>Any ideas would be greatly appreciated.
ML - 30 Jan 2005 12:24 GMT
Thanks very much. I managed to get it working as desired.
> Let's assume you define document properties in the template, name them
> Name1, Name2, and Street, create each one as Text (which corresponds
[quoted text clipped - 98 lines]
>>>>
>>>>Any ideas would be greatly appreciated.