不是代码编写服务,但我有点喜欢这个想法,所以就这样吧;这应该让您有一个良好的开端。
注意 1:您需要将其放入 .dotm 文件中,并最终在您的 PC (google) 上另存为全局模板。
注意 2:如果您打开超过 1 个文档,这将失败,因为只存储了 1 个密码 - 您可以将密码写为文档属性(您可以在保存和重新锁定之前检索并删除它)。
取决于您是否愿意将代码添加到 Normal.dotm 模板(我个人不喜欢)将影响您如何执行此操作。
如果不使用 Normal.dotm,那么您将需要设置一个全局模板并通过创建您自己的应用程序事件来触发代码,如下所述:https://wordmvp.com/FAQs/MacrosVBA/PseudoAutoMacros.htm
如果使用 Normal.dotm 则在 ThisDocument 添加:
Private Sub Document_Open()
MsgBox ActiveDocument.Name
Dim oDoc As Object
Set oDoc = ActiveDocument
unlocker oDoc
End Sub
并且(用于测试)在常规模块中添加以下内容(稍后您可能希望将其拆分为单独的代码单元):
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