Restrict/Lock bookmarks from editing in word
The following idea is tested for Word 2010. It should work for 2007 and 2013 as well but not for 2003.
I would suggest to use ContentControls
(called CC further in the text) together with Bookmarks
. Next, you will need to control one event which will check if user is selecting inside any of the ContentControl
. If so, we will show the message and/or move selection outside protected area.
Step 1st. Each of your bookmarks should be enclosed inside RichText ContentControl. You could do it manually for selected bookmarks or you can run the following simple code to do it for all bookmarks inside your active document.
(Important assumption! there are not any other ContentControls
in your document!)
Sub Add_Bookmark_CC()
Dim bookM As Bookmark
For Each bookM In ActiveDocument.Bookmarks
ActiveDocument.ContentControls.add wdContentControlRichText, bookM.Range
Next
End Sub
2nd step. We will control one event: Document_ContentControlOnEnter
. Go to ThisDocument
module in your Document VBAProject and create the following event (see some comments inside the code):
Private Sub Document_ContentControlOnEnter(ByVal ContentControl As ContentControl)
Debug.Print Now, ContentControl.Range.Bookmarks.Count
If ContentControl.Range.Bookmarks.Count > 0 Then
'Optional message box for user
MsgBox "There is bookmark inside this area which you should not change. " & _
vbNewLine & "You will be moved out of this range"
'optionam selection change right after CC area
Dim newPos As Long
newPos = ContentControl.Range.End + 2
ActiveDocument.Range(newPos, newPos).Select
End If
End Sub
Alternative for step 1st and 2nd. If you don't want to use CC event you could add CC to each bookmarks with CC content protection. In this situation you only need 1st step and the following sub:
Sub Add_Bookmark_CC_Protected()
Dim bookM As Bookmark
Dim CC As ContentControl
For Each bookM In ActiveDocument.Bookmarks
Set CC = ActiveDocument.ContentControls.add(wdContentControlRichText, bookM.Range)
CC.LockContents = True
Next
End Sub
Final! As you can see there are some more possible combination of steps 1 and 2. The following code allows you to delete all CC if you need for any initial tests:
Sub Remove_All_CC()
Dim CC As ContentControl
For Each CC In ActiveDocument.ContentControls
CC.Delete
Next CC
End Sub
Protect your whole document using
'whole document readonly
ThisDocument.Protect Password:="password", NoReset:=False, Type:=wdAllowReadOnly
or
'only write in form fields (can't delete them, just fill them out)
ThisDocument.Protect Password:="mypassword", NoReset:=False, Type:=wdAllowOnlyFormFields
and now give some parts of the document free for editing:
ThisDocument.Bookmarks("myBookmark").Range.Editors.Add wdEditorEveryone
Selection.Range.Editors.Add wdEditorEveryone
Alternative (not tested)
don't protect your whole document, just restrict the bookmarks you want to lock
ThisDocument.Bookmarks("myBookmark").Range.Editors.Add wdEditorOwners
or
ThisDocument.Bookmarks("myBookmark").Range.Editors.Add "[email protected]"