vba 限制/锁定书签在 Word 中编辑

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21163867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 01:25:12  来源:igfitidea点击:

Restrict/Lock bookmarks from editing in word

vbams-wordword-vbabookmarksrestriction

提问by Mana

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

我有很多带有很多书签的word文档。我使用 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.

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

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

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

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

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?

哪种做我想要的。但无法实现/将其转换为 VBA 代码。有人也可以看到我如何使用它吗?

Thanks again.

再次感谢。

EDIT: office 2007 / 2010.

编辑:办公室 2007 / 2010。

采纳答案by Kazimierz Jawor

The following idea is tested for Word 2010. It should work for 2007 and 2013 as well but not for 2003.

以下想法已针对 Word 2010 进行了测试。它也适用于 2007 和 2013,但不适用于 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.

我建议将ContentControls(在文本中进一步称为 CC)与Bookmarks. 接下来,您需要控制一个事件,该事件将检查用户是否在任何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.

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

(Important assumption!there are not any other ContentControlsin your document!)

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

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 ThisDocumentmodule in your Document VBAProject and create the following event (see some comments inside the code):

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

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:

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

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:

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

Sub Remove_All_CC()

    Dim CC As ContentControl
    For Each CC In ActiveDocument.ContentControls
        CC.Delete
    Next CC
End Sub

回答by CommonGuy

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替代品(未测试)

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