user input to be entered into text boxes

D

Dave

Want I am hoping to do on a form letter where a name, date and a few
other things are entered more than once, is to use a few input boxes
to populate the various text boxes.
This is what I have got so far

fullname = InputBox("Enter the employees full name", "Name")
annidate = InputBox("Enter the anniversary date of the employee",
"Start Date")
hours = InputBox("Enter the total number of hours to be worked each
fortnight", "Fortnightly hours")

ActiveDocument.Variables("textbox1").Value = fullname
ActiveDocument.Variables("textbox2").Value = fullname
ActiveDocument.Variables("textbox3").Value = annidate
ActiveDocument.Variables("textbox4").Value = hours
ActiveDocument.Variables("text1").Value = hours

it ran through the macro fine, but nothing seemed to happen on the
document (actually a template if that makes a difference.

This should be quite simple but I can't seem to find the right
syntax.

Dave
 
G

Graham Mayor

Your macro assigns the values to document variables. To reproduce those
variables, you need docvariable fields in the text boxes e.g. {DocVariable
textbox1} will reproduce the content of the fullname string. You then need
to update those fields to show the correct data - which can be a bit of an
issue with text boxes as text boxes are not in the text layer of the
document. I would also suggest that you indicate the types of variable in
their names so as not to confuse them with built-in vba names. You don't
need to define the same data in two different variables. You can use the
same one twice (or more). Thus the following should do what you want, and
you can place the results with three docvariable fields i.e.
{ DocVariable varName }
{ DocVariable varAnnidate }
and
{ DocVariable varHours }
respectively



Dim sFullName, sAnnidate, sHours As String
Dim oVars As Variables
Dim oStory As Range
Dim oField As Field
Set oVars = ActiveDocument.Variables
sFullName = InputBox("Enter the employees full name", "Name")
sAnnidate = InputBox("Enter the anniversary date of the employee", "Start
Date")
sHours = InputBox("Enter the total number of hours to be worked each
fortnight", "Fortnightly hours")
'define the document variables from the input box strings
oVars("varName").Value = sFullName
oVars("varAnniDate").Value = sAnnidate
oVars("varHours").Value = sHours
'Update all the fields in the document
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
D

Dave

Thank you very much Graham.
It took me a little time to work out:

"To reproduce those variables, you need docvariable fields in the
text boxes e.g. {DocVariable
textbox1} will reproduce the content of the fullname string"

I could not enter a field into a text box and I thought I was missing
something obvious.
That something obvious was you can not enter a field into a text box.
Once I realised you need to insert the "docvariable" from the field
menu item it was straightforward.

I plan on using this in numerous templates in an attempt to "idiot
proof" them.

Thanks again,

Dave
 

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