Restrict/Lock bookmarks from editing in word

Mana picture Mana · Jan 16, 2014 · Viewed 7.3k times · Source

I have many word document with lots of bookmarks. I use VBA code to change these bookmarks with data from a DB.

The problem is, sometimes the users need to edit these documents, and they tend to accidentally delete/change my bookmarks, which leads to the VBA code not recognizing the bookmark anymore.

So basically, what i'm wondering is how i can restrict users from editing my bookmarks in a word document.

I don't need a super secure solution, just enough protection so that the user knows that, "i should not touch this part".

Thanks in advance for your answer..

EDIT:

I was reading on different forums, and came across this,

http://social.msdn.microsoft.com/Forums/office/en-US/f70ca604-bbdb-4b5a-8363-f9e126105e91/writeprotection-of-bookmarks-in-word?forum=vsto

Which sort of does what i want. but was not able to implement/convert it to VBA code. Can someone also see how i maybe can use it?

Thanks again.

EDIT: office 2007 / 2010.

Answer

Kazimierz Jawor picture Kazimierz Jawor · Jan 21, 2014

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