How to Restrict Word Documents to Form Filling Using VBA
Question details
The user needs to use a VBA script to restrict editing in a Word document so that users can only fill out designated form fields, ensuring existing revision protections are handled without errors.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Automating document protection settings for multiple files via macros to ensure uniform data entry without allowing structural edits.
- Observed behavior
- The goal is to programmatically unprotect a document from previous restrictions (like revision tracking) and correctly re-protect it specifically for form filling using VBA.
Before running the VBA script, ensure you have the correct password for any existing document protection, and save a backup copy of your Word document to prevent accidental data loss during macro testing.
Apply Form-Filling Protection Using VBA
Use a VBA macro to systematically unprotect the document from previous restrictions and apply the wdAllowOnlyFormFields protection type.
When processing multiple documents, a file might already have revision protection or other restrictions enabled. It is best practice to first remove any existing protection using the Unprotect method before applying the new restriction to avoid runtime errors.
Open your Word document and press 'Alt + F11' to launch the Visual Basic for Applications (VBA) editor.
In the left-hand Project Explorer pane, right-click your document name, select 'Insert', and click on 'Module'.
Enter the VBA script. Use 'oDoc.Unprotect Password:="YourPassword"' to clear existing restrictions, followed by 'oDoc.Protect Type:=wdAllowOnlyFormFields, Password:="YourPassword"' to restrict editing to forms.
Press 'F5' or click the 'Run' button in the toolbar to execute the script and update the document's protection settings.
Easily Restrict Documents to Form Filling in WPS Writer
While VBA macros are great for bulk processing, WPS Writer allows you to easily restrict editing and allow only form filling through its user-friendly interface, requiring absolutely no code.
- 1. Open Document: Launch WPS Writer and open the document containing your form fields.
- 2. Access Review Tab: Navigate to the 'Review' tab located on the top ribbon menu.
- 3. Open Restrict Editing: Click on the 'Restrict Editing' button to open the protection options pane on the right.
- 4. Set Editing Restrictions: Check the box for 'Allow only this type of editing in the document' and select 'Filling in forms' from the dropdown menu.
- 5. Enforce Protection: Click 'Start enforcing protection', enter a secure password, and click 'OK'.

Frequently Asked Questions
Can I unprotect a Word document in VBA without the original password?
No, Word requires the correct password to unprotect a document programmatically using the Unprotect method. If the document is password-protected and the script does not provide the correct password, the macro will throw an error.
What exactly does wdAllowOnlyFormFields do in VBA?
The wdAllowOnlyFormFields is a built-in VBA enumeration constant. When applied via the Protect method, it locks the structural content of the document and restricts user interaction to only modifying form controls like text fields, checkboxes, and dropdown lists.
Why do I get a runtime error when applying form field protection via VBA?
This typically occurs if the document is already protected with another method (such as tracked changes or read-only). You must always call the Unprotect method to clear existing restrictions before applying a new ProtectionType like wdAllowOnlyFormFields.
Do I need to check if the document is protected before calling Unprotect?
Yes, it is highly recommended. You can check the ProtectionType property of the document first. If ProtectionType is not wdNoProtection, then execute the Unprotect command to prevent errors.




