PDA

View Full Version : Word Command Button to Copy a Table



SNewsome
02-03-2010, 08:28 AM
On the attached form, I want users to click the Add Defendant button that will copy the Defendant table below it so they can add as many defendants as they need. I also want them to be able to click the Add Charge button to add additional charge rows to each table.

I don't have a clue how to do this. Any help would be appreciated.

lucas
02-03-2010, 09:21 AM
I had to split your table to get it to work: see attached.

fumei
02-03-2010, 10:09 AM
That will work, but it can be done much simpler.

As both commandbuttons essentially do the same thing - add a row to a table - it is easier to make ONE procedure that adds a row to a given table. If the specific table is passed as parameter (an object), the row_adding procedure can handle ANY table. Like this:
Sub MakeNewRow(oTable As Table)
Dim NewRow As Row
Dim oCell As Cell

ActiveDocument.Unprotect
oTable.Range.Cells(oTable.Range.Cells.Count).Select
Selection.MoveRight Unit:=wdCell
Set NewRow = oTable.Rows(oTable.Rows.Count)
For Each oCell In NewRow.Cells
oCell.Range.FormFields.Add Range:=oCell.Range, _
Type:=wdFieldFormTextInput
oCell.Range.FormFields(1).Select
With Dialogs(wdDialogFormFieldOptions)
.Name = "Col" & oCell.ColumnIndex & "Row" & oCell.RowIndex
.Execute
End With
Next
NewRow.Cells(1).Select
ActiveDocument.Protect wdAllowOnlyFormFields, NoReset:=True, _
Password:=""
End Sub


Now the commandbutton procedure can simply make a table object of the specific table, and pass it to the row_adding procedure. Like this:
Sub CommandButton1_Click()
Dim oTable As Table
Set oTable = ActiveDocument.Tables(2)
Call MakeNewRow(oTable)
End Sub

Sub CommandButton2_Click()
Dim oTable As Table
Set oTable = ActiveDocument.Tables(3)
Call MakeNewRow(oTable)
End Sub
CommandButton1 processes Table(2), Commandbutton2 processes Table(3).

Regarding the row_adding procedure, while using counters will work, it is not strictly needed.

Selecting the last cell - note I use Range.Cells(Range.Cells.Count) rather than Cell(Row, Column) - then using a MoveRight automatically creates a new row.

This is the same as selecting the last cell manually and pressing Tab.

Thus, a new row.

A row object can be made for this new row using Range.Rows.Count.

A For Each statement can use:

1. a Cell object for each cell in the new row
2. insert a formfield into the cell
3. select it (cell.Range.Formfields(1).Select)
4. write a .Name with the same structure ("Col" number "Row" number) using the cell .ColumnIndex and cell.RowIndex.

At the end, the first cell is select to bring the focus (once protected for forms again), to that formfield. This is an assumption on my part. Any formfield could be the focus, but I thought the first one would likely be the one desired.

The advantage of this route is the re-useability of the row_adding procedure - no need to write it twice (for each commandbutton) - plus it makes the commandbutton procedures themselves cleaner (make a table object and pass it on). Chunking code like this also makes debugging easier (the commandbutton code is very basic, so any problems will be in ONE place - the Called procedure).

SNewsome
02-08-2010, 07:09 AM
Thank you so much for your help. Your level of expertise far exceeds mine. Why doesn't anything happen when I click on the Add Defendant or Add Charge buttons? New rows do not appear.

lucas
02-08-2010, 07:53 AM
I just downloaded the files from post #2 and post#3 and they both work when you click the buttons.

fumei
02-08-2010, 10:36 AM
The buttons should work. Are you saving they do not at all? NOTHING happens? Are there any error messages?

SNewsome
03-17-2010, 08:40 AM
Please look at the attached Word form. It's pretty self-explanatory what I'm trying to do. Everything works fine except for the "Add Defendant" button. When this button is pressed, I want it to copy the defendant (table2) and charge (table3) tables so another defendant and his/her charges can be entered. Right now, it simply adds a row to the defendant table.

Can anyone help with this? Thanks.

lucas
03-17-2010, 09:14 AM
That will be difficult as it involves copying a button to make the 3rd table work.

fumei
03-18-2010, 08:49 AM
"When this button is pressed, I want it to copy the defendant (table2) and charge (table3) tables so another defendant and his/her charges can be entered."

Ummm, copy it where?

I take it, you actually mean:

1. Copy the Defendent table, and Charge table...somewhere.
2. Clear the original table so a new Defendent and Charge can be entered.