I am over mind head

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.
 
J

Jezebel

Here's another way to do it --

Dim pTable As Word.Table

Set pTable = Selection.Tables(1)
With pTable.Rows(pTable.Rows.Count).Range
.Copy
.Collapse Direction:=wdCollapseEnd
.Paste
End With



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.
 
G

Greg Maxey

Jezebel,

I don't get it. I replaced my Addrow() macro with yours and the result is:

RunTime Error '4605'
The property or method is not available because no text is selected.

P.S. is your e-mail restored?

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Here's another way to do it --

Dim pTable As Word.Table

Set pTable = Selection.Tables(1)
With pTable.Rows(pTable.Rows.Count).Range
.Copy
.Collapse Direction:=wdCollapseEnd
.Paste
End With



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.
 
G

Greg Maxey

Jezebel,

Even if I could get your method to work, I am not sure, but I sense that by simply copying the row above that I would not create 'bookmarks' for the new formfields created.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Here's another way to do it --

Dim pTable As Word.Table

Set pTable = Selection.Tables(1)
With pTable.Rows(pTable.Rows.Count).Range
.Copy
.Collapse Direction:=wdCollapseEnd
.Paste
End With



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.
 
J

Jezebel

The only pre-condition for running the code is that the selection is in a table. You're right about the field naming. You could deal with that along these lines --

Dim pTable As Word.Table
Dim pRange1 As Word.Range
Dim pRange2 As Word.Range
Dim pFormField As Word.FormField
Dim pIndex As Long

Set pTable = Selection.Tables(1)
Set pRange1 = pTable.Rows(pTable.Rows.Count).Range
With pRange1
.Copy
.Collapse Direction:=wdCollapseEnd
.Paste
End With
Set pRange2 = pTable.Rows(pTable.Rows.Count).Range

For pIndex = 1 To pRange1.FormFields.Count
pRange2.FormFields(pIndex).Select
With Dialogs(wdDialogFormFieldOptions)
.Name = pRange1.FormFields(pIndex).Name & " copy" '< -- whatever naming function you need
.Execute
End With
Next





Jezebel,

Even if I could get your method to work, I am not sure, but I sense that by simply copying the row above that I would not create 'bookmarks' for the new formfields created.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Here's another way to do it --

Dim pTable As Word.Table

Set pTable = Selection.Tables(1)
With pTable.Rows(pTable.Rows.Count).Range
.Copy
.Collapse Direction:=wdCollapseEnd
.Paste
End With



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.
 
G

Greg Maxey

Jezebel,

That code was close. I needed to create a third range set to pRange1.Duplicate as pRange1 is becomes collapsed.

This seems to work reasoably well:

Sub NewRow()
Dim pTable As Word.Table
Dim oRng1 As Word.Range
Dim oRng2 As Word.Range
Dim oRng3 As Word.Range
Dim pFormField As Word.FormField
Dim i As Long
If MsgBox("Do you want to create a new row?", vbQuestion + vbYesNo, "Create New Row") = vbYes Then
ActiveDocument.Unprotect
Set pTable = Selection.Tables(1)
Set oRng1 = pTable.Rows(pTable.Rows.Count).Range
Set oRng3 = oRng1.Duplicate
With oRng1
.Copy
.Collapse Direction:=wdCollapseEnd
.Paste
End With
Set oRng2 = pTable.Rows(pTable.Rows.Count).Range
For i = 1 To oRng1.FormFields.Count
oRng2.FormFields(i).Select
With Dialogs(wdDialogFormFieldOptions)
.Name = oRng3.FormFields(i).Name & "_Copy_" & i
.Execute
End With
Next
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

The only pre-condition for running the code is that the selection is in a table. You're right about the field naming. You could deal with that along these lines --

Dim pTable As Word.Table
Dim pRange1 As Word.Range
Dim pRange2 As Word.Range
Dim pFormField As Word.FormField
Dim pIndex As Long

Set pTable = Selection.Tables(1)
Set pRange1 = pTable.Rows(pTable.Rows.Count).Range
With pRange1
.Copy
.Collapse Direction:=wdCollapseEnd
.Paste
End With
Set pRange2 = pTable.Rows(pTable.Rows.Count).Range

For pIndex = 1 To pRange1.FormFields.Count
pRange2.FormFields(pIndex).Select
With Dialogs(wdDialogFormFieldOptions)
.Name = pRange1.FormFields(pIndex).Name & " copy" '< -- whatever naming function you need
.Execute
End With
Next





Jezebel,

Even if I could get your method to work, I am not sure, but I sense that by simply copying the row above that I would not create 'bookmarks' for the new formfields created.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Here's another way to do it --

Dim pTable As Word.Table

Set pTable = Selection.Tables(1)
With pTable.Rows(pTable.Rows.Count).Range
.Copy
.Collapse Direction:=wdCollapseEnd
.Paste
End With



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.
 
G

Greg Maxey

Jezebel,

I also thought it would be a good idea flag any calculation fields that are copied as I can't think of anyway to modify the expression with VBA. Can you?

Sub NewRow()
Dim pTable As Word.Table
Dim oRng1 As Word.Range
Dim oRng2 As Word.Range
Dim oRng3 As Word.Range
Dim oFormField As Word.FormField
Dim bCalcFlag As Boolean
Dim i As Long
If MsgBox("Do you want to create a new row?", vbQuestion + vbYesNo, "Create New Row") = vbYes Then
ActiveDocument.Unprotect
bCalcFlag = False
Set pTable = Selection.Tables(1)
Set oRng1 = pTable.Rows(pTable.Rows.Count).Range
Set oRng3 = oRng1.Duplicate
With oRng1
.Copy
.Collapse Direction:=wdCollapseEnd
.Paste
End With
Set oRng2 = pTable.Rows(pTable.Rows.Count).Range
For i = 1 To oRng1.FormFields.Count
Set oFormField = oRng1.FormFields(i)
With oFormField
If .Type = wdFieldFormTextInput Then
If Not bCalcFlag And .TextInput.Type = 5 Then
bCalcFlag = True
MsgBox "You must edit expressions in any new calculation fields."
End If
End If
End With
oRng2.FormFields(i).Select
With Dialogs(wdDialogFormFieldOptions)
.Name = oRng3.FormFields(i).Name & "_Copy_" & i
.Execute
End With
Next
If Not bCalcFlag Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If
End If
End Sub


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

The only pre-condition for running the code is that the selection is in a table. You're right about the field naming. You could deal with that along these lines --

Dim pTable As Word.Table
Dim pRange1 As Word.Range
Dim pRange2 As Word.Range
Dim pFormField As Word.FormField
Dim pIndex As Long

Set pTable = Selection.Tables(1)
Set pRange1 = pTable.Rows(pTable.Rows.Count).Range
With pRange1
.Copy
.Collapse Direction:=wdCollapseEnd
.Paste
End With
Set pRange2 = pTable.Rows(pTable.Rows.Count).Range

For pIndex = 1 To pRange1.FormFields.Count
pRange2.FormFields(pIndex).Select
With Dialogs(wdDialogFormFieldOptions)
.Name = pRange1.FormFields(pIndex).Name & " copy" '< -- whatever naming function you need
.Execute
End With
Next





Jezebel,

Even if I could get your method to work, I am not sure, but I sense that by simply copying the row above that I would not create 'bookmarks' for the new formfields created.

--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Here's another way to do it --

Dim pTable As Word.Table

Set pTable = Selection.Tables(1)
With pTable.Rows(pTable.Rows.Count).Range
.Copy
.Collapse Direction:=wdCollapseEnd
.Paste
End With



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.
 

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

Similar Threads


Top