"Jamie" wrote:
> I’m working in Word 2003. I’m creating a template with a table and form
> fields. The table make up is this:
>
> 1st row has 4 columns
> Date: (text in column 1) – form field (column 2) – Client Name: (text in
> column 3) – form field (column 4)
>
> 2nd row has 4 columns:
> Referred To: (text in column 1) – form field (column 2) – Follow-Up Date:
> (text in column 3) – form field (column 4)
>
> 3rd row as 1 column:
> Comments: form field
>
> I would like to attach an on exit macro to the Comments form field that asks
> the user if they would like to add another client. If they click on Yes, the
> document would unlock and all three rows are copied with the form fields –
> however, the form fields need to be blank. Once copied the template would
> lock again.
>
> The user may need to do this many times; add clients.
Instead of using an onExit macro, I would use two toolbar buttons that the
user would clik on to add/delete clients.
As you wrote, in both cases the code would unlock the document, then add a
client at the end of the table (or under next comment line, which would
require a bit mnore code), or delete the current client. Then you would lock
the document.
The easiest wayt o add a blank client section is to create it as an AutoText
and then use code to insert the autotext.
Option Explicit
Sub AddClient()
Dim rgeTable As Range
With ActiveDocument
.Unprotect 'Password if necessary
Set rgeTable = .Tables(1).Range
With rgeTable
.Collapse wdCollapseEnd
.Text = "ClientForm" 'Autotext name
.InsertAutoText
End With
.Protect wdAllowOnlyFormFields, True ',Password if necessary
End With
End Sub
Sub DeleteClient()
With ActiveDocument
If Selection.Tables(1).Rows.Count = 3 Then
MsgBox "You must have at least one client.", _
vbExclamation, "Cancelled"
Exit Sub
Else
If Selection.Range.Paragraphs(1).Range.ParagraphFormat.Style <>
"ClientName" Then
MsgBox "You can only use this function with the cursor
positioned in the client name text.", _
vbExclamation, "Cancelled"
Exit Sub
End If
.Unprotect 'Password if necessary
Selection.Rows(1).Delete
Selection.Rows(1).Delete
Selection.Rows(1).Delete
End If
LeaveSub:
.Protect wdAllowOnlyFormFields, True ',Password if necessary
End With
End Sub