VB

N

Nigel Forge

I have written some VB code (not very neat) to unprotect a form and add a row
then format the fields in that row as text form fields. It works except it
only works on row 6 as I cannot find a way to determine the last row in three
tables, and when I run it the previous data in the preceding rows is deleted.
Anyone with any ideas please about how to determine the last table row then
insert below and also to protect the existing data from being deleted. (The
user would only need to use this if they run out of rows so they have already
added a few rows of data at this point).

Thanks
 
D

Doug Robbins - Word MVP

In the properties dialog for the last formfield in the last row, set the
macro to be run on exit to the following

Sub AddNewRow()
Dim Response
Response = MsgBox("Do you want to add another row?", vbQuestion + vbYesNo,
"Add Row")
If Response = vbYes Then ' User chose Yes.
With ActiveDocument
.Unprotect
Set oTable = Selection.Tables(1) 'Select the appropriate table
iCol = oTable.Columns.Count 'Record the last column number
oTable.Rows.Add
iRow = oTable.Rows.Count 'Record the last row number
For i = 1 To iCol
.FormFields.Add Range:=oTable.Cell(iRow, i).Range,
Type:=wdFieldFormTextInput
oTable.Cell(iRow, i).Range.FormFields(1).Select
With Dialogs(wdDialogFormFieldOptions) 'and name it
.Name = "Col" & i & "Row" & iRow 'eg Col1Row2
.Execute 'apply the changes
End With
Next i
With oTable
.Cell(iRow - 1, iCol).Range.FormFields(1).ExitMacro = ""
.Cell(iRow, iCol).Range.FormFields(1).ExitMacro = "AddNewRow"
End With
.Protect NoReset:=True, Type:=wdAllowOnlyFormFields 'Reprotect the
form
.FormFields("Col1Row" & iRow).Select 'and select the next field to
be completed
End With
Else ' User chose No.
Exit Sub
End If
End Sub


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
N

Nigel Forge

Thanks Doug - work like a dream. Appreciate the help very much.


--
Nigel Forge



Doug Robbins - Word MVP said:
In the properties dialog for the last formfield in the last row, set the
macro to be run on exit to the following

Sub AddNewRow()
Dim Response
Response = MsgBox("Do you want to add another row?", vbQuestion + vbYesNo,
"Add Row")
If Response = vbYes Then ' User chose Yes.
With ActiveDocument
.Unprotect
Set oTable = Selection.Tables(1) 'Select the appropriate table
iCol = oTable.Columns.Count 'Record the last column number
oTable.Rows.Add
iRow = oTable.Rows.Count 'Record the last row number
For i = 1 To iCol
.FormFields.Add Range:=oTable.Cell(iRow, i).Range,
Type:=wdFieldFormTextInput
oTable.Cell(iRow, i).Range.FormFields(1).Select
With Dialogs(wdDialogFormFieldOptions) 'and name it
.Name = "Col" & i & "Row" & iRow 'eg Col1Row2
.Execute 'apply the changes
End With
Next i
With oTable
.Cell(iRow - 1, iCol).Range.FormFields(1).ExitMacro = ""
.Cell(iRow, iCol).Range.FormFields(1).ExitMacro = "AddNewRow"
End With
.Protect NoReset:=True, Type:=wdAllowOnlyFormFields 'Reprotect the
form
.FormFields("Col1Row" & iRow).Select 'and select the next field to
be completed
End With
Else ' User chose No.
Exit Sub
End If
End Sub


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

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