Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restrict/Lock bookmarks from editing in word

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.

like image 980
Mana Avatar asked Jan 16 '14 14:01

Mana


People also ask

How do I lock a bookmark in Word?

There is no way to do this in Word; there is no way to set a bookmark as "hidden" or to lock it in place. You might think that you can mark a bookmark as hidden because when you display the Bookmark dialog box (Insert | Bookmark) (See Figure 1.)

Can you restrict editing to parts of a Word document?

Add protection and mark the parts that can be changed. On the Review tab, in the Protect group, click Restrict Editing. In the Editing restrictions area, select the Allow only this type of editing in the document check box. In the list of editing restrictions, click No changes (Read only).

Can you lock pages in Word?

Click on Restrict editing in the Developer tab, click option 2 Editing restrictions and select Filling in forms in the drop down box. Then click on "Yes, Start Enforcing Protection". From there you should have an option to put in a password.


1 Answers

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
like image 52
Kazimierz Jawor Avatar answered Sep 24 '22 07:09

Kazimierz Jawor