Thread: Table in a Form
View Single Post
  #11   Report Post  
Posted to microsoft.public.word.tables
Rich White
 
Posts: n/a
Default Table in a Form

Here is a macro that I put together from two other suggestions that seems to
work for my situation. I have a form with multiple tables and I want the user
to be able to add rows to any of the tables if needed. In my case, the first
column in each table contains an autonumber and the remaining cells have form
text inputs. Therefore, the macro adds a new row, moves over one cell and
then adds form text inputs to all the remaining cells. Use this macro as an
"on exit" macro for the last cell in any table that you want to be
expandable. One thing I added that I didn't see the other solutions is that I
remove the on exit macro from the current last cell when adding a new row.
This ensures that only the last cell of the table has the expansion
capability.

Sub AddRow()
'
' AddRow Macro
' Macro created 1/19/2006 by Rich White
'
' Ask user if they want to add a new row to the table
response = MsgBox("Add New Row?", vbQuestion + vbYesNo)

' If yes, then insert a new row and add a form text input to each cell
' starting in the second column (first column contains item number)
If response = vbYes Then
Dim i As Integer
' Unprotect the document to make the table update
ActiveDocument.Unprotect
' Disable the AddRow macro that is currently in the last cell
' (since we're creating a new last cell)
Selection.Tables(1).Cell(Selection.Tables(1).Rows. Count, _
Selection.Tables(1).Columns.Count).Range.FormField s(1).ExitMacro
= ""
' Insert the new row
Selection.InsertRowsBelow 1
Selection.Collapse (wdCollapseStart)
' Add form text input to columns 2 through n
For i = 2 To Selection.Tables(1).Columns.Count
Selection.MoveRight Unit:=wdCell
Selection.FormFields.Add Range:=Selection.Range,
Type:=wdFieldFormTextInput
Next i
' Attach AddRow macro to last cell
Selection.Tables(1).Cell(Selection.Tables(1).Rows. Count, _
Selection.Tables(1).Columns.Count).Range.FormField s(1).ExitMacro
= "AddRow"
' Make second cell of new row active
Selection.Tables(1).Cell(Selection.Tables(1).Rows. Count,
2).Range.FormFields(1).Select
' Protect document
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If
End Sub

Regards,

Rich

"HiDbLevel" wrote:

I have created a form for other users to use, and in it I would like to have
a way of automatically inserting a new/fresh row in the table so as to allow
for multiple entries. Due to the nature of the form, I can't just have
twenty rows just ready to go, I need to somehow have rows created as the user
needs them.