Word VBA文档解除编辑限制

被锁定的文档可以用以下代码来解除限制编辑。

Sub 解除限制编辑()
    Dim docpath As String
    Dim doc As Document
    Dim newdoc As Document
    If ActiveDocument.ProtectionType = -1 Then Exit Sub
    cledata = MsgBox("为了解除限制,程序需关闭并重新打开本文档。是否继续?", vbYesNo, "提示信息")
    If cledata = vbYes Then
        Set doc = ActiveDocument
        docpath = doc.FullName
        doc.Close False
        Set newdoc = Documents.Add
        Selection.InsertFile fileName:=docpath, Range:="", ConfirmConversions:= _
                False, Link:=False, Attachment:=False
        newdoc.SaveAs2 docpath
    End If
End Sub

原理:

通常文档限制编辑,可以通过导入内容到新的文档就可以解除锁定。VBA利用这个原理,后台新建一个文档,并且把限制编辑的文档导入到新的文档中,最后保存新文档替换旧文档即可。

发表评论