Article ID: 181108
Article Last Modified on 8/21/2007
173707 OFF97: How to Run Sample Code from Knowledge Base Articles
Sub ProtectForm()
' ******************************************
' ProtectForm Macro.
' Toggles protection for the active document
' when the Protect Form button on the forms
' toolbar is clicked.
' ******************************************
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
Else
ActiveDocument.Unprotect Password:=""
End If
End Sub
The following sample Visual Basic macro protects the active document without displaying the Protect dialog box. When you run this macro, it will reprotect the active document while maintaining previous form field values.
Sub ToolsProtectUnprotectDocument()
' ******************************************
' ToolsProtectUnprotectDocument Macro
' Sets protection for the active document
' when Protect Document or Unprotect Document
' is clicked on Tools menu.
' ******************************************
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
Else
ActiveDocument.Unprotect Password:=""
End If
End Sub
The following sample Visual Basic macro allows you to specify which sections to protect while maintaining previous form field values. You can assign this macro to a toolbar button or menu.
Sub ProtectNoReset()
Dim pDoc As Dialog
Dim x As Integer
On Error GoTo ProtectNoResetErr
' If the document is protected,
If ActiveDocument.ProtectionType <> wdNoProtection Then
' Unprotect the document.
ActiveDocument.Unprotect
End If
' Display the Protect Dialog box.
Set pDoc = Dialogs(wdDialogToolsProtectDocument)
x = pDoc.Display
' If Cancel was chosen, exit this procedure.
If x = 0 Then Exit Sub
' Protect the document.
ActiveDocument.Protect Password:=pDoc.DocumentPassword, _
NoReset:=True, Type:=2
ProtectNoResetErr: 'NOTE: This line MUST be left aligned.
If Err <> 0 Then MsgBox Err.Description
End Sub
Sub ProtectIt()
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If
End Sub
Sub FormsSpellCheck()
' If document is protected, Unprotect it.
If ActiveDocument.ProtectionType <> wdNoProtection Then
ActiveDocument.Unprotect Password:=""
End If
' Set the language for the document.
Selection.WholeStory
Selection.LanguageID = wdEnglishUS
' Perform Spelling/Grammar check.
If Options.CheckGrammarWithSpelling = True Then
ActiveDocument.CheckGrammar
Else
ActiveDocument.CheckSpelling
End If
' ReProtect the document.
If ActiveDocument.ProtectionType = wdNoProtection Then
ActiveDocument.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End If
End Sub
IMPORTANT NOTE: There are some differences in Visual Basic for Applications between Microsoft Word 97 for Windows and Microsoft Word 2000 due to the Enabled Language Settings feature in Microsoft Office 2000. One of the differences is noted in the above macro. To correctly check the spelling of a document after setting the LanguageID, you must set the NoProofing property in Word 2000.
For additional information about how to do this in Microsoft Word 2000, click the article number below
to view the article in the Microsoft Knowledge Base:
191028 WD2000: How to Retain Information Typed into a Form Field When You Protect a Form
163425 WD97: Macro Programming Resources
163435 VBA: Programming Resources for Visual Basic for Applications
Additional query words: wordcon kbmacro vba checkspelling
Keywords: kbdtacode kbinfo kbprogramming KB181108