AddRow to table

J

jlute

I have an AddRow macro that is giving me fits. It works fine in other
templates however I just created a template in which it doesn't
behave. The table has 5 columns and the macro is supposed to prompt
to
add another row per the Exit of a prticular field control. If
affirmed
the new row is created with the appropriate control fields and then
the cursor to go to the first field in the first column of the added
row. Everything works fine except the cursor ends up in the second
column. I can't see why this is. Can anyone help? Thanks much in
advance!

Sub AddRow()
'
' AddRow Macro
'
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 oRowID As Long
Dim i As Long
If MsgBox("Do you want to add another row?", vbQuestion + vbYesNo,
"Add Another 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
oRowID = pTable.Rows.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 & "_" & oRowID
.Execute
End With
Next
If Not bCalcFlag Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields,
NoReset:=True
End If
pTable.Rows.Last.Cells(1).Range.Fields(1).Result.Select
End If
End Sub
 
D

David Horowitz

Hi. Well, it all seems to hinge on your last line of code:
pTable.Rows.Last.Cells(1).Range.Fields(1).Result.Select
This tries to select the first form field in the first cell in the last row
of your table.
I would check to make sure that the first cell in the last row of your table
does contain a form field.
Other than that, I would carefully check to make sure that no code is moving
the Selection AFTER AddRow returns...
HTH
 
G

Graham Mayor

You might also want to remove the exit macro from the form field after it
has run to avoid complications when the user goes backwards in the form.
See the two sample macros at http://www.gmayor.com/word_vba_examples.htm
1. Add a row to a table in a protected form
2. An alternative method of adding a row to a protected table

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
L

lbb4ever

This is great... almost exactly what I was looking for. One question
though: I would like the new fields to be named as the fields in row
2. As it is now, the fields are being copied from the row above the
inserted row, but "_oRowID" is being tacked on to the end. This
results in "Field" (this being the original field name in row 2),
"Field_3" (row 3), "Field_3_4" (row 4), "Field_3_4_5" (row 5), etc.

I've tried modifying the code (see below) to get do this, but I get an
error that the command failed. What am I missing? You can see by the
comments what I started with (your code) and what I've played with.

With Dialogs(wdDialogFormFieldOptions)
.Name = oRng1.FormFields(i).Name & oRowID - 1 ' oRng3.FormFields
(i).Name & "" & oRowID - 1 ' oRng3.FormFields(i).Name & "" & oRowID
.Execute
End With

Thanks!
lbb4ever
 
G

Graham Mayor

It took me a while to realise that you were not the original questioner,
however if you want to add a row to the end and name the new row based on
the names in the second row of the table then the following based on the
code on my web site will do that. Watch out for premature line wrapping when
copying the code.


Sub AddARow()
'Run on exit from the last form field in
'the last row of the table
Dim oTable As Table
Dim oRng As Range
Dim oCell As Range
Dim oLastCell As Range
Dim sResult As String
Dim iRow As Integer
Dim iCol As Integer
Dim CurRow As Integer
Dim i As Long
With ActiveDocument
.Unprotect Password:="" 'Unprotect document
Set oTable = .Tables(1) 'Select the appropriate table
iRow = oTable.Rows.Count 'Record the last row number
iCol = oTable.Columns.Count 'Record the last column number
Set oLastCell = oTable.Cell(iRow, iCol).Range 'Record the last cell
sResult = oLastCell.FormFields(1).Result 'Get the value in the last cell
Set oRng = oTable.Rows(iRow).Range 'Add the last row to a range
oRng.Copy 'Copy the row
oRng.Collapse wdCollapseEnd 'collapse the range to its end.
oRng.Select 'the end of the table
Selection.Paste 'Paste the row at the end of the table
CurRow = iRow + 1 'Record the new last row
For i = 1 To iCol 'Repeat for each column
Set oCell = oTable.Cell(CurRow, i).Range 'process each cell in the
row
oCell.FormFields(1).Select 'Select the first field in the cell
With Dialogs(wdDialogFormFieldOptions) 'and name it after the names
in the second row
.name = oTable.Cell(2, i).Range.FormFields(1).name & "_" &
CurRow
.Execute 'apply the changes
End With
Next i
'Select the formfield in the last cell of the previous row
oLastCell.FormFields(1).Select
With Dialogs(wdDialogFormFieldOptions)
.Exit = "" 'and remove the exit macro
.Execute 'apply the changes
'but note that this clears the value from the cell
End With
oLastCell.FormFields(1).Result = sResult 'so restore the result of the
cell
.Protect NoReset:=True, Password:="", _
Type:=wdAllowOnlyFormFields 'Reprotect the form
.FormFields(oTable.Cell(2, 1).Range.FormFields(1).name & "_" &
CurRow).Select 'and select the next field to be completed
End With
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
L

lbb4ever

Thank you for the quick reply :) I was perusing your site yesterday.
Good stuff.

I got it to work between a combination of your code and the orig
author's code. Now if I can just get the cmd buttons to do the same
thing ;)

The modified code is as follows:
Sub AddRow()
'
' AddRow Macro
'
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 oRowID As Long
Dim i As Long
If MsgBox("Do you want to add another row?", vbQuestion + vbYesNo,
"Add Another 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
oRowID = pTable.Rows.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 = pTable.Cell(2, i).Range.FormFields(1).Name & oRowID - 1
.Execute
End With
Next
If Not bCalcFlag Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If
pTable.Rows.Last.Cells(1).Range.Fields(1).Result.Select
End If

End Sub
 

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