Form Field Properties + Add Row to Protected Table.

S

spunkymuffmonkey

Firstly, thank you for reading this post and looking at my problem.

I am currently developing a protected form for users to complete that
contains several separated tables within it.

One of the tables is used to record all children in the household, as most
households do not have more than 4 children I have preset the table to
contain 5 rows (one row is the header), but there is the possibility that
more than 4 children will need to be recorded in this table.

Therefore, I have used the advice and tips provided in other threads and
included an exit macro for the last field in the bottom row which will allow
users to add a new row.

My problem is that I would like to configure the on exit macro for the last
field in the new row so that the process can repeat itself as often as is
required by the user, but also clear the on exit macro for the previous row.

The code I have pasted below changes the exit macro for the last field in
the table at the very bottom of the document, not the last field in the
current table and I cannot work out how to change the code it to cater for
me. Do I need to go down the route of creating a name for the new fields and
using the name in the Macro and if so is this something somebody can
illuminate me regarding.

Any advice and suggestions will be greatly appreciated.

Thanks again.

Code:

Sub addrow()

response = MsgBox("Add new row?", vbQuestion + vbYesNo)
If response = vbYes Then
ActiveDocument.Unprotect Password:=""

Selection.InsertRowsBelow 1
Selection.Collapse (wdCollapseStart)
myRow = Selection.Information(wdStartOfRangeRowNumber)

Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Enabled = True
End With
Selection.MoveRight Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Enabled = True
End With
Selection.MoveRight Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Enabled = True
End With
Selection.MoveRight Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Enabled = True
.ExitMacro = "addrow"
End With
Selection.MoveLeft Unit:=wdCell
Selection.MoveLeft Unit:=wdCell
Selection.MoveLeft Unit:=wdCell
Selection.MoveLeft Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
myCount = ActiveDocument.Range.FormFields.Count
With ActiveDocument.FormFields(myCount)
.Enabled = True

Selection.MoveLeft Unit:=wdCell
Selection.MoveRight Unit:=wdCell
End With

ActiveDocument.Protect NoReset:=True, Password:="",
Type:=wdAllowOnlyFormFields
End If

End Sub
 
L

Lene Fredborg

You have set MyCount equal to the total number of FormFields in the document
(ActiveDocument.Range.FormFields.Count). Therefore, the following code:

With ActiveDocument.FormFields(MyCount)
.Enabled = True
.ExitMacro = "addrow"
End With

assigns the “addrow†macro to the very _last_ FormField in the document, not
the one you just added (correspondingly, Enabled is set on the last
FormField). If you change the code line:

With ActiveDocument.FormFields(MyCount)

to:

With Selection.Cells(1).Range.FormFields(1)

the macro should be assigned to the correct FormField.


Your macro seems to be created mainly by using the macro recorder. Below you
will find another, shorter and more flexible macro – see the comments in the
code (and note that there will almost always be many ways to solve the same
problem):


Sub AddRow()

Dim oRow As Row
Dim n As Long

Response = MsgBox("Add new row?", vbQuestion + vbYesNo)
If Response <> vbYes Then Exit Sub

With ActiveDocument
'Prevent error if document is not protected
If .ProtectionType <> wdNoProtection Then
.Unprotect Password:=""
End If
End With

Set oRow = Selection.Tables(1).Rows.Add

'The following inserts a TextFormField in all cells in the new row
'Change 1 to the number of the first cell into which a FormField must be
inserted
With oRow
For n = 1 To .Cells.Count
'Insert TextFormField in cell
.Range.FormFields.Add Range:=.Cells(n).Range,
Type:=wdFieldFormTextInput

'If last cell, assign ExitMacro
If n = .Cells.Count Then
.Cells(n).Range.FormFields(1).ExitMacro = "AddRow"
End If

'Select the first FormField in the new row
.Cells(1).Range.FormFields(1).Select
Next n
End With

ActiveDocument.Protect NoReset:=True, Password:="",
Type:=wdAllowOnlyFormFields

'Clean up
Set oRow = Nothing

End Sub

--
Regards
Lene Fredborg - Microsoft MVP (Word)
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
 
S

spunkymuffmonkey

Many thanks for taking the time to help, with your suggestion I managed to
rectify my own code, but have utilised your code in an attempt to learn from
it.

Kind regards.

Alex
 

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