VBA & Macro Problems

Fix Word VBA Errors When Deleting Sections in Protected Forms

Rana GarciaRana Garcia Nov 25, 2026 868 views

Question details

Fix VBA errors when a Word macro deletes sections from a protected form and then attempts to reprotect it.

Fix Word VBA Errors When Deleting Sections in Protected Forms
Product
Microsoft Word
Device & OS
not provided
Scenario
A form field exit macro deletes sections and then restores form protection.
Observed behavior
Word can return error 5224 when protection is applied with the selection in an invalid location. A subsequent attempt to reprotect may return error 4641, indicating that the ToolsProtectDocument statement is disabled.
Before you start

In Word, open the form's VBA project and identify the exit macro, the sections it deletes, and a form field that remains afterward. Check whether the document uses a protection password and whether the Developer tab is available.

Solution 1Recommended

Select a valid form field before reprotecting the document

Use this approach when error 5224 occurs after section deletion leaves the selection in an invalid location.

1
Open the exit macro

In Word, open Developer > Visual Basic and locate the macro assigned to the form field's exit event.

2
Identify a surviving field

Find a form field that remains after the macro deletes the sections, preferably the next field in the form. Confirm its bookmark name in the form field's properties.

3
Select the field

After the section-deletion code and before the protection command, add: ActiveDocument.FormFields("Text1").Select. Replace Text1 with the name of the surviving field.

4
Reapply form protection

While the document is unprotected, apply protection after selecting the field, for example: ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True. Supply the password if the form's protection requires one.

5
Test the form

Return to the document, use the form field to run its exit macro, and confirm that the sections are deleted and form protection is restored.

Select a valid form field before reprotecting the document
The field must still exist: Selecting a field that was deleted or has a different name will cause another error. Choose a field that remains in every section-deletion path.
Free Microsoft Office alternative

Edit Word Documents with WPS Writer

WPS Office is a free, lightweight alternative with a familiar interface for opening and editing Word documents. Its format compatibility helps you move everyday documents to WPS, though it does not fix Microsoft Word VBA errors.

Open and edit Microsoft Word DOC and DOCX filesReview form content and document sections in WPS WriterUse a familiar interface for everyday document editingMove compatible documents between Office and WPS Office
microsoft office alternative - wps office

Frequently Asked Questions

Why does error 5224 occur after my macro deletes sections?

Deleting sections can leave the selection in a location that is invalid for the Protect command. After deletion, select a form field that still exists before calling ActiveDocument.Protect.

How do I find the name to use in FormFields?

In Word, open the form field's properties and check its bookmark name. Use that name in ActiveDocument.FormFields("FieldName").Select, and make sure the field is not deleted by the macro.

Does a second dropdown need different exit macro syntax?

Not necessarily. Check that its exit macro is assigned in the dropdown's form field properties, then use Developer > Visual Basic to verify that the macro reaches its code and selects a valid field before reprotection.

What should I check if Word reports error 4641?

Check ActiveDocument.ProtectionType and the status under Developer > Restrict Editing. If the macro uses ToolsProtectDocument, try ActiveDocument.Protect after selecting a surviving field and confirming that the document is unprotected.

Will WPS Office fix my Word VBA macro?

No. WPS Office can open and edit compatible Word documents, but it does not fix errors in Microsoft Word. Test the macro in Word and verify any macro compatibility before migrating the form.