简体   繁体   English

VBA Word:删除打开时的编辑限制,关闭时再次限制

[英]VBA Word: remove editing restriction at opening and restrict again when closing

Is it possible to set a macro that would trigger each time I open a word document and check if it has an editing restriction. 是否可以设置一个宏,该宏将在每次打开Word文档并检查是否有编辑限制时触发。 If so, try password from a list of passwords (hardcoded). 如果是这样,请尝试从密码列表(硬编码)中输入密码。 In case one password is successfull, keep it in memory, remove restriction, and re-apply the restriction when I close the document. 万一密码成功,请将其保存在内存中,删除限制,然后在关闭文档时重新应用该限制。

In this way, if I always use the same password for the documents I use and restrict, I could open them on my computer as if there was no restriction, but the restriction would still apply to other users. 这样,如果我对使用和限制的文档始终使用相同的密码,则可以在计算机上打开它们,就好像没有限制一样,但是该限制仍然适用于其他用户。

Note: the macro in Private Sub Document_Open() would need to trigger on all documents I open from my computer only. 注意:Private Sub Document_Open()中的宏仅在我从计算机打开的所有文档上都需要触发。 Documents must be .docx and not .docm. 文件必须是.docx,而不是.docm。

Thank you. 谢谢。

Not a code writing service but I kinda like the idea so here goes; 不是代码编写服务,但我有点喜欢这个想法, this should get you off to a good start. 这应该使您有个良好的开端。

Note1: You will need to put this into a .dotm file and ultimately save as a global template on your PC (google). 注意1:您将需要将其放入.dotm文件中,并最终将其另存为PC上的全局模板(google)。

Note2: This will fail if you open more than 1 doc because only 1 password is stored - you could write the password as a document property (which you would retrieve & delete before saving and relocking). 注意2:如果您打开多个文档,这将失败,因为仅存储了1个密码-您可以将密码写为文档属性(在保存和重新锁定之前可以检索并删除)。

Depending on whether or not you are happy to add code to the Normal.dotm template (personally I'm not) will influence how you do this. 取决于您是否乐意将代码添加到Normal.dotm模板(我个人不是)将影响您的操作方式。

If NOT using Normal.dotm then you will need to setup a global template AND trigger the code by creating your own application events as described here: https://wordmvp.com/FAQs/MacrosVBA/PseudoAutoMacros.htm 如果不使用Normal.dotm,则需要设置一个全局模板并通过创建自己的应用程序事件来触发代码,如下所述: https : //wordmvp.com/FAQs/MacrosVBA/PseudoAutoMacros.htm

If using Normal.dotm then in ThisDocument add: 如果使用Normal.dotm,则在ThisDocument添加:

Private Sub Document_Open()
    MsgBox ActiveDocument.Name
    Dim oDoc As Object
    Set oDoc = ActiveDocument
    unlocker oDoc
End Sub

And (for testing) in a regular module add the following (you'll likely want to split this into separate units of code later): 并(在测试中)在常规模块中添加以下内容(您稍后可能会希望将其拆分为单独的代码单元):

Sub unlocker(ByVal docToUnlock As Document)
    If Not docToUnlock.Type = wdTypeDocument Then
        ' this is a template, don't try anything
        MsgBox "Not a doc"
        GoTo endOfSub
        Else
        MsgBox "Is a doc"
    End If


    Dim passWords() As String
    passWords = Split("pw1,pw2,pw3", ",")

    Dim iLoop As Long
    iLoop = LBound(passWords)

    On Error GoTo err_Test:

    Do While Not ActiveDocument.ProtectionType = wdNoProtection
        If iLoop > UBound(passWords) Then Exit Do

        oldpassword = passWords(iLoop)

        ActiveDocument.Unprotect oldpassword
        iLoop = iLoop + 1
    Loop

    If Not ActiveDocument.ProtectionType = wdNoProtection Then
        ' unable to unlock document, quit
        oldpassword = vbNullString
        MsgBox "Failed to Unlock"
        GoTo endOfSub
    Else
        MsgBox "Unlocked"
    End If

    ' Do Stuff

    If Not oldpassword = vbNullString Then
        ActiveDocument.Protect wdAllowOnlyReading, Password:=oldpassword
    End If

endOfSub:
    Exit Sub

err_Test:
    If Err.Number = 5485 Then
        ' ignore error due to wrong password
        Err.Clear
        Resume Next
    Else
        ' handle unexpected error
    End If

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM