Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adding a row to a protected table

411 views
Skip to first unread message

Divinite

unread,
Sep 3, 2010, 4:09:37 PM9/3/10
to
I'm using a script from http://www.gmayor.com/word_vba_examples.htm
(An alternative method of adding a row to a protected table) to add
rows to a project management form I'm developing. We have a few tables
on the form that would need it (task lists, project documents, etc).

I'm currently calling the script in the last input cell of each table.
When I try to tab to the next cell, it adds a row, but to the first
table in the document, not where the cursor is. It should be selecting
the currently active table, but it's not.

I'm using Word 2007. Any help to get this working would be great!
Thank you in advance.

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

Jay Freedman

unread,
Sep 3, 2010, 7:54:50 PM9/3/10
to
Your code has the line

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

where the ".Tables(1)" combines with the preceding "With
ActiveDocument" so it's equivalent to

Set oTable = ActiveDocument.Tables(1)

That's why it's always adding the row to the first table in the
document. The proper expression to make it operate on the table that
contains the cursor (a.k.a. the Selection) is

Set oTable = Selection.Tables(1)

Everything else can remain the same.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

0 new messages