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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Macro in Checkbox to Add AutoText Row to Table

Status
Not open for further replies.

cheriberi

Technical User
May 27, 2003
60
US
(I forgot to add a subject line, so I've resubmitted my posting with a subject.)

I am working on a Word template that contains many tables and is locked so only fields can be updated. This template needs to do the following:

For one specific table, the user wants to be able to click something and add between 1 and 5 additional rows at the end of the table. Since the row being added needs to have specific text in it, I saved the row as an AutoText entry (called Add1--TriggeredEvents).

I then added two hard returns below the table (and formatted them the same as the text in the table row so the styles won't change when the row is added). I added a bookmark at the second hard return (called AddTriggeredEvents) since I found that if the bookmark was at the hard return just below the table, it moved into the table when the row was added. This would cause problems if the user decided later to add another row.

Finally, I added 5 checkbox formfields at the top of the table where the user could select between 1 and 5 rows to add. For the first checkbox which will add one row to the table, I gave it a bookmark called Add1TriggeredEvents. For the second checkbox which will add two rows to the table, I gave it a bookmark called Add2TriggeredEvents.

I need a macro that will run when the checkbox formfield is exited that will determine whether or not the checkbox has been "checked":

If the Add1TriggeredEvents checkbox is checked, nothing should happen when the user exists the checkbox.

If the Add1TriggeredEvents checkbox is not checked, the macro needs to:

-- Go to the AddTriggeredEvents bookmark at the second paragraph mark below the table

-- Move up one line (so the insertion point is just below the table and the bookmark is not relocated)

-- Insert the Add1--TriggeredEvents AutoText entry (keeping the original formatting in the new row)

I tried to create the macro myself, but when I "check" the checkbox and move out of it so the macro will fire, I end up with an error at the code to add the AutoText entry.

Here's the code I have come up with so far:

Sub Add1_TriggeredEvents()

' Unlock the document
ActiveDocument.Unprotect

' If checkbox is checked, go to bookmark (at end of table) and add row
If ActiveDocument.FormFields("Add1TriggeredEvents").CheckBox.Value = True Then
Selection.GoTo What:=wdGoToBookmark, Name:="AddTriggeredEvents"
With ActiveDocument.Bookmarks
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
Selection.MoveUp Unit:=wdLine, Count:=1
Application.DisplayAutoCompleteTips = True
NormalTemplate.AutoTextEntries("Add1--TriggeredEvents").Insert Where:= _
Selection.Range
End If

' Lock the document
ActiveDocument.Protect Type:=wdAllowOnlyFormFields

' Return to location at end of table
Selection.GoTo What:=wdGoToBookmark, Name:="AddTriggeredEvents"

' If checkbox is not checked, do nothing
If ActiveDocument.FormFields("Add1TriggeredEvents").CheckBox.Value = False Then
' Nothing
End If

End Sub

I need help getting the code above to work. I also need to know how to alter the code so it would add 2 rows if the Add2TriggeredEvents checkbox was selected?

Thanks!

Cheryl
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top