G
Greg Maxey
Hi Sensei's
After many hours of trial and tribulation, I have reached the limit of my skills and seek your assistance. I am working on a macro to add a new row to a table in a protected form on exit from the last column/last row of the field of the table. I am *almost* successful if the cells of the table only contain one formfield. I am trying to enhance this general macro to duplicate a row if it contains more that one formfield. I am close as it the preceeding row contains a text field then a checkbox I can create a new row with a text field and checkbox but the problem I am having is the checkbox in the new row is before the text field.
Here is the code so far:
Sub Addrow()
Dim rownum As Long, i As Long, j As Long, x As Long, y As Long
Dim oRng As Word.Range
Dim pListArray() As String
Dim pType As String
Dim pExit As String
Dim pEntry As String
Dim pEnabled As Boolean
Dim pCalcOnExit As Boolean
Dim pDefText As String
Dim pDefCheck As Boolean
Dim pStatusText As String
Dim pHelpText As String
Dim oRgField As FormField
Dim myField As FormField
Dim oTbl As Table
Set oTbl = Selection.Tables(1)
ActiveDocument.Unprotect
oTbl.Rows.Add
rownum = oTbl.Rows.Count
For i = 1 To oTbl.Columns.Count
'Set oRng = oTbl.Cell(rownum, i).Range
'oRng.Collapse wdCollapseStart
y = oTbl.Cell(rownum - 1, i).Range.FormFields.Count
For x = 1 To y
Set oRgField = oTbl.Cell(rownum - 1, i).Range.FormFields(x)
With oRgField
pType = .Type
pExit = .ExitMacro
pEntry = .EntryMacro
pEnabled = .Enabled
pDefText = .TextInput.Default
pCalcOnExit = .CalculateOnExit
If .Type = wdFieldFormCheckBox Then
pDefCheck = .CheckBox.Default
End If
pStatusText = .StatusText
pHelpText = .HelpText
End With
Select Case pType
Case wdFieldFormDropDown
Set oRng = oTbl.Cell(rownum, i).Range
oRng.Collapse wdCollapseStart
Set myField = ActiveDocument.FormFields.Add(Range:=oRng, _
Type:=wdFieldFormDropDown)
With myField
.ExitMacro = pExit
.EntryMacro = pEntry
.Enabled = pEnabled
.CalculateOnExit = pCalcOnExit
.StatusText = pStatusText
.HelpText = pHelpText
End With
For j = 1 To oTbl.Cell(rownum - 1, i).Range.FormFields(1).DropDown.ListEntries.Count
ReDim Preserve pListArray(j)
pListArray(j) = oTbl.Cell(rownum - 1, i).Range.FormFields(1).DropDown.ListEntries(j).Name
Next j
For j = 1 To UBound(pListArray)
myField.DropDown.ListEntries.Add pListArray(j)
Next j
'oRng.Collapse wdCollapseStart '*************
Case wdFieldFormTextInput
Set oRng = oTbl.Cell(rownum, i).Range
Set myField = ActiveDocument.FormFields.Add(Range:=oRng, _
Type:=wdFieldFormTextInput)
With myField
.ExitMacro = pExit
.EntryMacro = pEntry
.Enabled = pEnabled
.TextInput.Default = pDefText
.Result = pDefText
.CalculateOnExit = pCalcOnExit
.StatusText = pStatusText
.HelpText = pHelpText
End With
'oRng.Collapse wdCollapseStart '*************
Case wdFieldFormCheckBox
Set oRng = oTbl.Cell(rownum, i).Range
Set myField = ActiveDocument.FormFields.Add(Range:=oRng, _
Type:=wdFieldFormCheckBox)
With myField
.ExitMacro = pExit
.EntryMacro = pEntry
.Enabled = pEnabled
.CalculateOnExit = pCalcOnExit
.CheckBox.Default = pDefCheck
.StatusText = pStatusText
.HelpText = pHelpText
End With
'oRng.Collapse wdCollapseStart '*************
End Select
Next x
Next i
oTbl.Cell(oTbl.Rows.Count, 1).Range.FormFields(1).Select
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub
All you have to do is create a one row multicolumn table with fiels and Addrow set to run on exit from the last field.
After many hours of trial and tribulation, I have reached the limit of my skills and seek your assistance. I am working on a macro to add a new row to a table in a protected form on exit from the last column/last row of the field of the table. I am *almost* successful if the cells of the table only contain one formfield. I am trying to enhance this general macro to duplicate a row if it contains more that one formfield. I am close as it the preceeding row contains a text field then a checkbox I can create a new row with a text field and checkbox but the problem I am having is the checkbox in the new row is before the text field.
Here is the code so far:
Sub Addrow()
Dim rownum As Long, i As Long, j As Long, x As Long, y As Long
Dim oRng As Word.Range
Dim pListArray() As String
Dim pType As String
Dim pExit As String
Dim pEntry As String
Dim pEnabled As Boolean
Dim pCalcOnExit As Boolean
Dim pDefText As String
Dim pDefCheck As Boolean
Dim pStatusText As String
Dim pHelpText As String
Dim oRgField As FormField
Dim myField As FormField
Dim oTbl As Table
Set oTbl = Selection.Tables(1)
ActiveDocument.Unprotect
oTbl.Rows.Add
rownum = oTbl.Rows.Count
For i = 1 To oTbl.Columns.Count
'Set oRng = oTbl.Cell(rownum, i).Range
'oRng.Collapse wdCollapseStart
y = oTbl.Cell(rownum - 1, i).Range.FormFields.Count
For x = 1 To y
Set oRgField = oTbl.Cell(rownum - 1, i).Range.FormFields(x)
With oRgField
pType = .Type
pExit = .ExitMacro
pEntry = .EntryMacro
pEnabled = .Enabled
pDefText = .TextInput.Default
pCalcOnExit = .CalculateOnExit
If .Type = wdFieldFormCheckBox Then
pDefCheck = .CheckBox.Default
End If
pStatusText = .StatusText
pHelpText = .HelpText
End With
Select Case pType
Case wdFieldFormDropDown
Set oRng = oTbl.Cell(rownum, i).Range
oRng.Collapse wdCollapseStart
Set myField = ActiveDocument.FormFields.Add(Range:=oRng, _
Type:=wdFieldFormDropDown)
With myField
.ExitMacro = pExit
.EntryMacro = pEntry
.Enabled = pEnabled
.CalculateOnExit = pCalcOnExit
.StatusText = pStatusText
.HelpText = pHelpText
End With
For j = 1 To oTbl.Cell(rownum - 1, i).Range.FormFields(1).DropDown.ListEntries.Count
ReDim Preserve pListArray(j)
pListArray(j) = oTbl.Cell(rownum - 1, i).Range.FormFields(1).DropDown.ListEntries(j).Name
Next j
For j = 1 To UBound(pListArray)
myField.DropDown.ListEntries.Add pListArray(j)
Next j
'oRng.Collapse wdCollapseStart '*************
Case wdFieldFormTextInput
Set oRng = oTbl.Cell(rownum, i).Range
Set myField = ActiveDocument.FormFields.Add(Range:=oRng, _
Type:=wdFieldFormTextInput)
With myField
.ExitMacro = pExit
.EntryMacro = pEntry
.Enabled = pEnabled
.TextInput.Default = pDefText
.Result = pDefText
.CalculateOnExit = pCalcOnExit
.StatusText = pStatusText
.HelpText = pHelpText
End With
'oRng.Collapse wdCollapseStart '*************
Case wdFieldFormCheckBox
Set oRng = oTbl.Cell(rownum, i).Range
Set myField = ActiveDocument.FormFields.Add(Range:=oRng, _
Type:=wdFieldFormCheckBox)
With myField
.ExitMacro = pExit
.EntryMacro = pEntry
.Enabled = pEnabled
.CalculateOnExit = pCalcOnExit
.CheckBox.Default = pDefCheck
.StatusText = pStatusText
.HelpText = pHelpText
End With
'oRng.Collapse wdCollapseStart '*************
End Select
Next x
Next i
oTbl.Cell(oTbl.Rows.Count, 1).Range.FormFields(1).Select
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End Sub
All you have to do is create a one row multicolumn table with fiels and Addrow set to run on exit from the last field.