I have several form fields in a document in which I have created a UserForm to gather information to populate the various fields using bookmarks. I have the document protected but allow for edits in the various fields by the users. My issue is that if a user deletes all information in a form field it deletes the bookmark from the document. Is there a way to protect the bookmark so that if the information in the form field is deleted it doesn't delete the bookmark?
Didn't know the answer to this off the top of my head, but here's what GPT has to say about it:
Yes, when a user deletes all the text in a form field, Word removes the associated bookmark. To prevent this while allowing users to edit the fields, you can use VBA to restore the bookmark if it is deleted. Here are a few approaches:
**Solution 1: Use VBA to Restore the Bookmark** You can create a macro that runs when the document is opened or before printing to check for missing bookmarks and restore them if needed.
**Steps:** 1. Open the VBA Editor (`Alt + F11`).
2. Insert a new module.
3. Add the following code:
CodeSub RestoreBookmarks()
Dim bm As Bookmark
Dim rng As Range
Dim bmName As String
' List of bookmarks to check
Dim bmList As Variant
bmList = Array("Bookmark1", "Bookmark2", "Bookmark3") ' Update with your bookmark names
For Each bmName In bmList
If ActiveDocument.Bookmarks.Exists(bmName) = False Then
' Restore the bookmark at its original position
Set rng = ActiveDocument.Range
rng.Collapse wdCollapseEnd
rng.Text = "[Placeholder]" ' Insert placeholder text
ActiveDocument.Bookmarks.Add bmName, rng
End If
Next bmName
End Sub
4. Run this macro manually or set it to trigger on `Document_Open` or `Document_BeforeSave`.
---
**Solution 2: Use a Macro-Enabled Form Field** If you're using form fields (`Legacy Form Fields`), you can modify them to prevent the deletion of bookmarks.
**Steps:** 1. Double-click on the form field to open Form Field Options.
2. Check Calculate on exit.
3. Add the following VBA code in the VBA Editor (`Alt + F11`):
CodePrivate Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim bmName As String
bmName = ContentControl.Title ' Ensure the Content Control has a Title matching the bookmark name
' Restore bookmark if deleted
If ActiveDocument.Bookmarks.Exists(bmName) = False Then
ActiveDocument.Bookmarks.Add bmName, ContentControl.Range
End If
End Sub
This ensures the bookmark is automatically recreated when the user exits the field.
---
**Solution 3: Use a Content Control Instead of a Form Field** If possible, use Content Controls instead of Form Fields, as they retain bookmarks even when cleared.
**Steps:** 1. Unlock the document and replace the form fields with Rich Text Content Controls.
2. Set each control's Tag or Title to match the bookmark name.
3. Protect the document again.
Since Content Controls don't remove bookmarks when cleared, this method is the simplest if it suits your workflow.
---
**Summary** - Use VBA to check and restore missing bookmarks (`RestoreBookmarks` macro).
- Enable "Calculate on Exit" in form fields to force re-evaluation.
- Use Content Controls instead of form fields to avoid deletion issues.
Sorry, only students may add comments.
Click here for more
information on how you can set up an account.
If you are a Visitor, go ahead and post your reply as a
new comment, and we'll move it here for you
once it's approved. Be sure to use the same name and email address.
This thread is now CLOSED. If you wish to comment, start a NEW discussion in
Microsoft Word Forum.