Adding row to a table

B

Bigfoot17

I have appreciated the input of g. mayor and have found his "An alternative
method of adding a row to a protected table"
(http://www.gmayor.com/word_vba_examples.htm) to be quite helpful. It is
designed to be an 'exit macro' and I am trying to use it in a slightly
different way.

My issue is that as a user tabs through a form they will always come to the
last cell and tabbing to the next formfield just creates another row. So I
am attempting to set up a button (commandbutton) that will run the macro and
add an additional row 'on demand.' I have this working. But I have 5 tables
that I want to provide this option to. So five buttons - and the macro
copied 5 times. Next step - define a variable for the table number and pass
this to the macro so the code is entered just once. I run into some issues
here. Any guidance is appreciated.

Here is the AddARow macro:
Sub AddRow()
'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

Dim sPassword as String

sPassword = "" 'password to protect/unprotect form
With ActiveDocument
.Unprotect Password:=sPassword '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
.name = "Col" & i & "Row" & CurRow 'eg Col1Row2
.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:=sPassword, _
Type:=wdAllowOnlyFormFields 'Reprotect the form
.FormFields("Col1Row" & CurRow).Select 'and select the next field to be
completed
End With
End Sub
 
J

Jay Freedman

If the cursor is in the table that you want the macro to process, replace

Set oTable = .Tables(1) 'Select the appropriate table ************

with this:

If Selection.Information(wdWithInTable) Then
Set oTable = Selection.Tables(1)
Else
MsgBox "Place the cursor in a table first."
Exit Sub
End If

When the cursor (i.e., the Selection) is within a table, then
Selection.Tables(1) is the table that contains the cursor -- as opposed to
ActiveDocument.Tables(1), which is the first table in the document. This can
get a little weird if the table is really one of a set of nested tables, so
avoid creating nested tables in your template.

If for some reason you really, really need the actual index number of the
table within the collection ActiveDocument.Tables, you can use the technique
shown in http://www.word.mvps.org/FAQs/MacrosVBA/GetIndexNoOfPara.htm. But
that's a very rare requirement.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
B

Bigfoot17

I like this, and would like to take it a step further. I have five tables
out of 10 that I'd like to limit to adding rows, and they are on different
pages. So one commandbutton could control the adding of rows for all tables
but it would be cumbersome to go back and forth between pages to find the
comandbutton. Can I get 5 buttons running this code?
 
J

Jay Freedman

Generally I discourage the use of command buttons. They're ActiveX, which
doesn't work all that well in Word documents (see
http://msdn2.microsoft.com/en-us/library/aa140269(office.10).aspx,
especially the section "Appropriateness for the Task"). A better choice is a
MacroButton field
(http://www.word.mvps.org/FAQs/TblsFldsFms/UsingMacroButton.htm) in each of
the five tables, all of which launch the same macro.

If you decide on command buttons anyway, each of them must have its own
Click procedure. But if you store your macro as a separate procedure, each
of the Click procedures can call it
(http://www.word.mvps.org/FAQs/MacrosVBA/ProcArguments.htm).
 
F

Fumei2 via OfficeKB.com

Trying to follow this. Do you want one action (a commandbutton) to add rows
to ALL of the five tables? Just one? Which one? if it is the one the
Selection is in, then there is already code posted to do that.

If you want to be able to select one of the five tables to add a row to, this
can be done.

1. bookmark each of the five tables you want to action, and give that
bookmark a useful name: Table1, Clientnames, Yadda, Whatever, Sure.

2. Now for the commandbutton (and frankly, I would prefer to use a shortcut
key or a icon on a toolbar), you can use:

Sub CommandButton1_Click
' or Sub AddTableRow() if you are going to use a shortcut key

Dim oTable As Table
Dim strName As String
strName = InputBox(“Table name please.â€)
Select Case strName
Case “Table1â€
Set oTable = ActiveDocument.Bookmarks("Table1").Range.Tables(1)
oTable.Rows.Add
Case “Clientnamesâ€
Set oTable = ActiveDocument.Bookmarks("Clientnames").Range.Tables
(1)
oTable.Rows.Add
Case “Yaddaâ€
Set oTable = ActiveDocument.Bookmarks("Yadda").Range.Tables(1)
oTable.Rows.Add
Case “Whateverâ€
Set oTable = ActiveDocument.Bookmarks("Whatever").Range.Tables(1)
oTable.Rows.Add
Case “Sureâ€
Set oTable = ActiveDocument.Bookmarks("Sure").Range.Tables(1)
oTable.Rows.Add
Case Else
MsgBox “You have entered an invalid table name. Try Again.â€
End Select
End Sub

That way, you can perform the action (adding a row) from anywhere, you do not
need to find a commandbutton; AND the action can be performed on the tables
(and only the tables) you want to action.

Gerry

I like this, and would like to take it a step further. I have five tables
out of 10 that I'd like to limit to adding rows, and they are on different
pages. So one commandbutton could control the adding of rows for all tables
but it would be cumbersome to go back and forth between pages to find the
comandbutton. Can I get 5 buttons running this code?
If the cursor is in the table that you want the macro to process, replace
[quoted text clipped - 92 lines]
 
B

Bigfoot17

I took some time to go through these options and the Macrobutton has some
appeal. It is not as difficult to work with, and is easier to implement in my
project. I appreciate these links and greatly appreciate the time you have
taken to assist me.
 
B

Bigfoot17

I know it kind of weird to reply to your own post but in the past week I have
come across another trick that I think is worth passing along. I have used
the MACROBUTTON and it is displayed on the form as a shaded field. I decided
to use a white font color in the MACROBUTTON which contrasts with the shaded
field, and since the shading does not print the MACROBUTTON appears to vanish
when printed.
 

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