限制/锁定书签在 Word 中编辑 [英] Restrict/Lock bookmarks from editing in word

查看:77
本文介绍了限制/锁定书签在 Word 中编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多带有很多书签的 Word 文档.我使用 VBA 代码通过数据库中的数据更改这些书签.

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

问题是,有时用户需要编辑这些文档,他们往往会不小心删除/更改我的书签,导致 VBA 代码不再识别书签.

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.

所以基本上,我想知道的是如何限制用户在 Word 文档中编辑我的书签.

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".

预先感谢您的回答..

我在不同的论坛上阅读,遇到了这个,

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=对比

哪种类型可以满足我的需求.但无法实现/将其转换为 VBA 代码.有人也可以看看我可以如何使用它吗?

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?

再次感谢.

办公室 2007/2010.

office 2007 / 2010.

推荐答案

以下想法已针对 Word 2010 进行了测试.它应该也适用于 2007 和 2013,但不适用于 2003.

我建议将 ContentControls(在文本中进一步称为 CC)与 Bookmarks 一起使用.接下来,您需要控制一个事件,该事件将检查用户是否在任何 ContentControl 内进行选择.如果是这样,我们将显示消息和/或将选择移到保护区外.

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.

第一步.您的每个书签都应包含在 RichText ContentControl 中.您可以为选定的书签手动执行此操作,也可以运行以下简单代码为活动文档中的所有书签执行此操作.

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.

(重要假设!您的文档中没有任何其他ContentControls!)

(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

第二步.我们将控制一个事件:Document_ContentControlOnEnter.转到 Document VBAProject 中的 ThisDocument 模块并创建以下事件(请参阅代码中的一些注释):

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

第 1 步和第 2 步的替代方法.如果您不想使用 CC 事件,您可以将 CC 添加到每个具有 CC 内容保护的书签中.在这种情况下,您只需要第一步和以下子:

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

最终!如您所见,步骤 1 和 2 有更多可能的组合.如果您需要任何初始测试,以下代码允许您删除所有 CC:

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

这篇关于限制/锁定书签在 Word 中编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆