How to Apply Word Document Protection With Section-Specific Permissions via VBA
🔍 WiseChecker

How to Apply Word Document Protection With Section-Specific Permissions via VBA

You need to protect parts of a Word document while leaving other sections editable by specific users or groups. Standard document protection in Word applies to the entire document and does not allow fine-grained control over individual sections. This article explains how to use Visual Basic for Applications (VBA) to apply section-specific protection that restricts editing in some sections while permitting changes in others. You will learn the VBA code structure, how to set passwords per section, and how to assign editing permissions to specific users.

Key Takeaways: Section-Specific Document Protection via VBA

  • Tools > Macro > Visual Basic Editor (Alt+F11): Open the VBA editor to write and run protection macros.
  • ActiveDocument.Sections(index).ProtectedForForms = True: Lock a specific section so only form fields can be edited.
  • ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=”password”: Apply password-protected editing restrictions to the entire document while preserving section-level form field settings.
  • ActiveDocument.Sections(index).Range.Editors.Add Editor:=”username”: Grant edit permissions to specific users for a given section.

ADVERTISEMENT

How Section-Specific Protection Works in Word

Word’s built-in document protection (Review > Restrict Editing) applies to the whole document. You can choose to allow only comments, tracked changes, or filling in forms. However, this method does not let you lock Section 1 while leaving Section 2 fully editable. VBA provides a workaround by combining form-field protection with section-level permissions.

The core concept uses the ProtectedForForms property of a Section object. When set to True, that section behaves as if it contains only form fields — no other content can be edited. By setting this property to False for sections you want to remain editable, you create a mixed-protection document. You then call the Protect method on the Document object with wdAllowOnlyFormFields to enforce the section-level settings.

Prerequisites

Before writing VBA code, ensure the following:

  • You have a Word document divided into sections using section breaks (Layout > Breaks > Next Page, Continuous, etc.).
  • You know the index numbers of the sections you want to protect. Section 1 is the first section in the document.
  • You have the VBA editor enabled. Go to File > Options > Customize Ribbon and check the Developer tab. Then click Developer > Visual Basic.
  • If you plan to assign permissions to specific users, those users must have Microsoft accounts or be part of the same organization using Information Rights Management (IRM).

Steps to Apply Section-Specific Protection Using VBA

Follow these steps to create a macro that locks Section 1 and Section 3 while leaving Section 2 editable. Adjust the section indices to match your document.

  1. Open the VBA Editor
    Press Alt+F11 to open the Visual Basic for Applications editor. In the Project Explorer, locate your document (e.g., Project (Document1)).
  2. Insert a new module
    Click Insert > Module to create a blank code module.
  3. Write the protection macro
    Paste the following code into the module:
    Sub ProtectSections()
    Dim doc As Document
    Set doc = ActiveDocument

    ' Unprotect the document first if already protected
    If doc.ProtectionType <> wdNoProtection Then
    doc.Unprotect Password:=""
    End If

    ' Set ProtectedForForms for specific sections
    ' Section 1: protected
    doc.Sections(1).ProtectedForForms = True
    ' Section 2: editable
    doc.Sections(2).ProtectedForForms = False
    ' Section 3: protected
    doc.Sections(3).ProtectedForForms = True

    ' Apply document-level protection to enforce form-field only editing
    doc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="MyPassword123"

    MsgBox "Protection applied. Sections 1 and 3 are locked; Section 2 is editable."
    End Sub

  4. Run the macro
    Press F5 while the cursor is inside the macro, or go to Run > Run Sub/UserForm. A message box confirms the protection is applied.
  5. Test the protection
    Try to edit text in Section 1 or Section 3. Word should prevent changes. Switch to Section 2 and verify that you can edit freely. If the document is password-protected, you must enter the password to unprotect it later.

Assigning Permissions to Specific Users

To allow only certain users to edit a section while keeping it locked for others, add the following code inside the macro after setting ProtectedForForms but before calling doc.Protect.

  1. Add a user to a section’s Editors collection
    Use the Editors.Add method with the user’s email or username:
    doc.Sections(2).Range.Editors.Add Editor:="user@example.com"
    This grants edit permission to that user for Section 2 even when the document is protected.
  2. Remove all editors from a protected section
    To ensure no one except the owner can edit a protected section, clear the Editors collection:
    doc.Sections(1).Range.Editors.DeleteAll
  3. Apply protection with user permissions
    After setting editors, call doc.Protect Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="MyPassword123". Users added as editors can modify content in their assigned sections.

ADVERTISEMENT

Common Issues When Using VBA for Section Protection

ProtectedForForms Does Not Lock the Section

If a section remains editable after setting ProtectedForForms = True, you likely forgot to call doc.Protect on the document. The ProtectedForForms property only takes effect when the document is protected with wdAllowOnlyFormFields. Always include the doc.Protect line after setting section properties.

Macro Runs but Protection Is Not Enforced After Closing and Reopening

Document protection settings are saved with the file. When you reopen the document, the protection remains active. However, the ProtectedForForms per-section settings are also preserved. To verify, open the document, go to Review > Restrict Editing, and check that “Filling in forms” is selected. If the pane shows “No protection,” run the macro again. Consider adding an AutoOpen macro to reapply protection automatically.

Permission Error When Adding Editors

The Editors.Add method requires the document to use Information Rights Management (IRM) or the user must be authenticated via a Microsoft account. If you receive a runtime error, ensure that the document is not already protected with a different type. Unprotect the document first, then add editors, and finally reapply protection.

Item Standard Word Restrict Editing VBA Section-Specific Protection
Scope Entire document Individual sections
User permissions Not supported per section Supported via Editors collection
Password One password for whole document One password for whole document, but sections behave differently
Editing allowed Comments, tracked changes, or form fields only Full editing in unprotected sections; form fields only in protected sections
Requires VBA No Yes, to set initial configuration

You can now apply section-specific document protection in Word using VBA. Start by identifying the sections in your document and writing a macro that sets ProtectedForForms for each section. Remember to call doc.Protect with wdAllowOnlyFormFields to enforce the settings. For advanced scenarios, assign editors to specific sections using the Editors.Add method. To automate protection every time the document opens, place the macro in the ThisDocument module and use the Document_Open event.

ADVERTISEMENT