MS Office Forum / Word / Programming / May 2006
I am over mind head
|
|
Thread rating:  |
Greg Maxey - 20 May 2006 05:49 GMT 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.
 Signature Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Jezebel - 20 May 2006 06:31 GMT 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.
-- Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Greg Maxey - 20 May 2006 06:57 GMT 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?
 Signature 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
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:uwFDPj8eGHA.3468@TK2MSFTNGP03.phx.gbl... 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.
-- Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Greg Maxey - 20 May 2006 07:02 GMT 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.
 Signature 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
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:uwFDPj8eGHA.3468@TK2MSFTNGP03.phx.gbl... 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.
-- Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Jezebel - 21 May 2006 01:56 GMT 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.
"Jezebel" <warcrimes@whitehouse.gov> wrote in message news:OI1WV68eGHA.3484@TK2MSFTNGP02.phx.gbl... 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
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:uwFDPj8eGHA.3468@TK2MSFTNGP03.phx.gbl... 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.
-- Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Greg Maxey - 21 May 2006 02:47 GMT 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
 Signature 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
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:OIux5L9eGHA.1260@TK2MSFTNGP05.phx.gbl... 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.
"Jezebel" <warcrimes@whitehouse.gov> wrote in message news:OI1WV68eGHA.3484@TK2MSFTNGP02.phx.gbl... 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
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:uwFDPj8eGHA.3468@TK2MSFTNGP03.phx.gbl... 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.
-- Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Greg Maxey - 21 May 2006 03:27 GMT 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
 Signature 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
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:OIux5L9eGHA.1260@TK2MSFTNGP05.phx.gbl... 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.
"Jezebel" <warcrimes@whitehouse.gov> wrote in message news:OI1WV68eGHA.3484@TK2MSFTNGP02.phx.gbl... 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
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:uwFDPj8eGHA.3468@TK2MSFTNGP03.phx.gbl... 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.
-- Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
Greg Maxey - 20 May 2006 20:54 GMT Jezebel,
I did get passed the error I was getting using your proposed method modified as follows:
Sub NewRow() Dim pTable As Word.Table If MsgBox("Do you want to create a new row?", vbQuestion + vbYesNo, "Create New Row") = vbYes Then Set pTable = Selection.Tables(1) ActiveDocument.Unprotect With pTable.Rows(pTable.Rows.Count).Range .Copy .Collapse Direction:=wdCollapseEnd .Paste End With ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True End If End Sub
This is certainly much less code than using code to recontruct each field inividually and will acurately replicate multiple fields in a cell. The only drawback that I see is that copied fields are not created with bookmarks. A few weeks ago, I scratched together some code to globally rename formfields in a document. I suppose this could be adapted and rename each field in the table with a generic sequenced bookmark name. Can you think of something better?
Sub NewRow() Dim pTable As Word.Table If MsgBox("Do you want to create a new row?", vbQuestion + vbYesNo, "Create New Row") = vbYes Then Set pTable = Selection.Tables(1) ActiveDocument.Unprotect With pTable.Rows(pTable.Rows.Count).Range .Copy .Collapse Direction:=wdCollapseEnd .Paste End With NameFormFields pTable ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True End If End Sub Sub NameFormFields(oTable As Word.Table) Dim oFrmFlds As FormFields Dim pIndex As Long Dim i As Long Dim j As Long Dim k As Long pIndex = 0 i = 0 j = 0 k = 0 Set oFrmFlds = oTable.Range.FormFields For pIndex = 1 To oFrmFlds.Count oFrmFlds(pIndex).Select Select Case oFrmFlds(pIndex).Type Case wdFieldFormTextInput i = i + 1 With Dialogs(wdDialogFormFieldOptions) .Name = "TableText" & i .Execute End With Case wdFieldFormCheckBox j = j + 1 With Dialogs(wdDialogFormFieldOptions) .Name = "TableCheck" & j .Execute End With Case wdFieldFormDropDown k = k + 1 With Dialogs(wdDialogFormFieldOptions) .Name = "TableDropDown" & k .Execute End With Case Else 'Do Nothing End Select Next pIndex End Sub
 Signature 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
"Greg Maxey" <gmaxey@mvps.oSCARrOMEOgOLF> wrote in message news:uwFDPj8eGHA.3468@TK2MSFTNGP03.phx.gbl... 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.
-- Greg Maxey/Word MVP See: http://gregmaxey.mvps.org/word_tips.htm For some helpful tips using Word.
|
|
|