Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add extra fields in MS Word Forms

Status
Not open for further replies.

djburnheim

Technical User
Jan 22, 2003
71
0
0
AU
I'm using the built in "Forms" tool in MS Word to create a report template. The template has several tables in it that could be anywhere from 1 row to 50. Rather than create 50 rows straight off I'm trying to dynamacly create rows as they are needed. The last cell in each row has a Text Form Field and I've created a macro that prompts the user to create another row which runs when the user exits the field (see below)...this bit is working but the bit I'm having trouble with is assigning the same Exit macro to the new field that is created in the new row. Any ideas would be greatly appreciated.

Code:
Sub AddComment()
    Set Doc = ActiveDocument
    Response = MsgBox("Add another comment?", vbYesNo, "Comments")
        If Response = vbYes Then
            If Doc.ProtectionType <> wdNoProtection Then Doc.Unprotect
                Selection.MoveRight Unit:=wdCell
                Selection.FormFields.Add Range:=Selection.Range, Type:= _
                    wdFieldFormTextInput
                Selection.MoveRight Unit:=wdCell
                Selection.FormFields.Add Range:=Selection.Range, Type:= _
                    wdFieldFormTextInput
                Doc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
        End If
End Sub
 
Hi djburnheim,

You should be able to pick up the exit macro assigned to the field you are leaving and assign it to the new field, something like this...
Code:
Sub AddComment()
    Set Doc = ActiveDocument
    Response = MsgBox("Add another comment?", vbYesNo, "Comments")
        If Response = vbYes Then
            If Doc.ProtectionType <> wdNoProtection Then Doc.Unprotect
                [blue]macro = doc.FormFields(Selection.Bookmarks(1)).exitmacro[/blue]
                Selection.MoveRight Unit:=wdCell
                Selection.FormFields.Add Range:=Selection.Range, Type:= _
                    wdFieldFormTextInput
                Selection.MoveRight Unit:=wdCell
                [blue]Set newformfield = [/blue]Selection.FormFields.Add Range:=Selection.Range, Type:= _
                    wdFieldFormTextInput
                [blue]newformfield.ExitMacro = macro[/blue]
                Doc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
        End If
End Sub

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top