PDA

View Full Version : Solved: Set value of textbox in macro



RigMan
06-08-2008, 07:45 PM
I have created a template that contains a lot of text boxes. Three of those text boxes allow the user to key a date. When the user keys the date in the first text box, I want to copy the date to the other two text boxes on field exit. My current code is as follows;



Sub setdate()
Set aDoc = ActiveDocument
If aDoc.ProtectionType <> wdNoProtection Then
aDoc.Unprotect
End If
aDoc.FormFields("Date2").Range.Text = ActiveDocument.FormFields("Date1").Range.Text
aDoc.FormFields("Date3").Range.Text = ActiveDocument.FormFields("Date1").Range.Text
aDoc.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub


The code works, except it replaces the text boxes labeled date2 and date3 with the value of date1 (i.e. the text box disappears and the date appears in it's place). If the user happens to click on date1 textbox again, and tabs out, I get an error saying 'The requested member of the collection does not exists' when it tries to set the value of date2 which has been deleted.

Any help? I'm working with Word 2002 SP3.

thanks,
Ronnie

Crooz
06-09-2008, 12:58 AM
Hi Ronnie,
Try this:


Private Sub TextBox1_Change()


'unprotect
TextBox2.Value = TextBox1.Value
TextBox3.Value = TextBox1.Value
'protect

End Sub


Works just fine on my PC.

fumei
06-09-2008, 05:48 AM
Crooz, the OP clearly stated formfields.


ActiveDocument.FormFields("Date1")



Formfields do NOT have a _Change event. You are using ActiveX controls which are utterly different things from formfields. Your code, and your example, does not apply to what was asked.

Also, even using ActiveX controls, it is usually better to NOT use .Value. If it is text that one wants, then use .Text. Being explicit is always better. Always.

Rigman, when getting text (even if it is Date) from a formfield, use .Result, NOT Range.Text.


aDoc.FormFields("Date2").Result

The reason that you are getting the error 'The requested member of the collection does not exists' is that it is true. It does not exist.


aDoc.FormFields("Date2").Range.Text = _
ActiveDocument.FormFields("Date1").Range.Text

The code above replaces the Range, which....removes the formfield, and replaces it with text.

You could keep them if you used .Result.
aDoc.FormFields("Date2").Result = _
ActiveDocument.FormFields("Date1").Result

However.....

WHY have formfields for the other two? Unless they are actually being used for user input, do not use formfields.

Formfields are for user input.

If you want to duplicate Date1, then simply go where you want it duplicated, and:

1. Press Ctrl-F9
2. type "Date1"
3. move your cursor somewhere else.

Done.

If your Date1 formfield is checked for Calculate on exit...badda bing, badda boom. Its value (.Result) will be automatically updated where ever you have done Step1 and Step 2.

You can do this as many times as you like, anywhere in the document. You could have a dozen duplicated values of Date1, anywhere you like.

Demo attached.

Crooz
06-09-2008, 06:24 AM
You're absolutely correct. I got a little ahead of myself.
Will pay more attention next time.

fumei
06-09-2008, 06:36 AM
It is easy enough. We all make assumptions at times. Not a big deal, except that it would not actually help the OP.

RigMan
06-09-2008, 08:17 AM
Thanks for the info guys. I'll try the suggestions when I get home tonight.

RigMan
06-09-2008, 02:51 PM
I choose this method;



aDoc.FormFields("Date2").Result = _
ActiveDocument.FormFields("Date1").Result


Because it's possible the 3 text boxes could require 3 different dates, although I've never seen a instance where it's necessary in this application. But by using the above method, the user can manual change the dates on the 2nd and 3rd textboxes if necessary. (the user in this case is my wife. So she can deal with it!).

Thanks,
Ronnie