PDA

View Full Version : Solved: Set Max Length After Adding text field



Dr@g0nfly
03-06-2007, 01:59 PM
Hi I have a protected form in Word 2000. I have inserted text fields into a table to allow the user to type the account numbers that will be affected by the form's contents into the document. I have only supplied two rows in the table but there is the possibility that there could be more needed. I add a command button with the following code attached to it:
Sub AddRow()
ActiveDocument.Unprotect
ActiveDocument.Tables(3).Rows.Add
Selection.Collapse Direction:=wdCollapseStart
Selection.FormFields.Add Range:=Selection.Range, Type:= _
wdFieldFormTextInput
Selection.MoveRight Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range, Type:= _
wdFieldFormTextInput
Selection.MoveRight Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range, Type:= _
wdFieldFormTextInput
Selection.MoveRight Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range, Type:= _
wdFieldFormTextInput
Selection.MoveRight Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range, Type:= _
wdFieldFormTextInput
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If
End Sub

This works great but I found during a trial that the user was able to type continuously in the new text fields whereas I had set the maximum length to 10 in the previous two rows, as well as the text type to "number". I need to restrict the fields but since they are being adding dynamically at the request of the user, I'm not sure how to set those properties... is there a way to set the maximum length and text field type using VBA code? I was hoping to add it to the code above so that when the fields are inserted, the code also tells the program which specific ones. I was thinking perhaps autotext might be a good place to start but the autotext refers to the Normaltemplate????

mdmackillop
03-06-2007, 03:51 PM
Sub AddRow()
With ActiveDocument.Tables(3)
.Rows.Add
For i = 1 To .Columns.Count
.Range.FormFields.Add Range:=.Cell(.Rows.Count, i).Range, Type:=wdFieldFormTextInput
With .Cell(.Rows.Count, i).Range.FormFields(1).TextInput
.Width = 10
.EditType Type:=wdNumberText, Default:="", Format:=""
End With
Next
End With
End Sub

fumei
03-06-2007, 06:36 PM
Dragonfly, please make note that Malcolm did NOT use Selection, nor move the Selection.

Neither should you be. There is likely no need to be Selecting anything, or moving it around in the cells. You can action table cells directly.

mdmackillop
03-07-2007, 05:33 AM
Dragonfly, please make note that Malcolm did NOT use Selection, nor move the Selection.

I'm learning! :thumb

Dr@g0nfly
03-07-2007, 06:20 AM
Thanks! I admit I cheated and did the action while recording a macro then copied the code out of the macro... that is probably why there is a "selection" action in the VB.

:cloud9: I'm definitely learning LOL :thumb

Dr@g0nfly
03-07-2007, 06:45 AM
I do receive an error when I run this code: Command failed
Perhaps the more helpful description however would be that the row is indeed inserted but only two of the five text fields were placed in the table. The fields are formatted correctly as number, max length 10, but there are only two in the row instead of the five I need. All your help is greatly appreciated!

Here I have added some unprotect code because I have password protected the document:
Sub AddRow()
ActiveDocument.Unprotect "relationship"
With ActiveDocument.Tables(3)
.Rows.Add
For i = 1 To .Columns.Count
.Range.FormFields.Add Range:=.Cell(.Rows.Count, i).Range, Type:=wdFieldFormTextInput
With .Cell(.Rows.Count, i).Range.FormFields(1).TextInput
.Width = 10
.EditType Type:=wdNumberText, Default:="", Format:=""
End With
Next
End With
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="relationship"
End If
End Sub

Thanks!

mdmackillop
03-07-2007, 10:23 AM
Can you post a copy of your table layout because the code should add as many textboxes as there are columns.

Dr@g0nfly
03-07-2007, 10:58 AM
I have attached the file: It seems to break down when making the field a number field (or editType). I think I can live with a text field but I suppose it would be nice to have it all locked up! Thanks!

Dr@g0nfly
03-08-2007, 09:56 AM
I gave up on the number designation but the rest works GREAT! Thanks!