使用 VBA 取消保护文件夹中的多个 word 文档

Unprotect multiple word documents inside a folder using VBA

我有多个word文档,需要在开发者模式下对它们进行限制。

我运行脚本使用wscript传递一个文件夹作为参数,但是它会报错

Dim strFolder
Const xlTypePDF = 0
strFolder = WScript.Arguments(0)

if  Wscript.Arguments.Count > 0 Then

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objWord = CreateObject("Word.Application")
    Set objFolder = objFSO.GetFolder(strFolder)

    For Each Fil In objFolder.Files
        Set objFile = objFSO.GetFile(Fil)

        Set objDoc = objWord.Documents.Open(Fil,,TRUE)
        dirPath = objFSO.GetParentFolderName(objFile)
        fileBaseName = objFSO.GetBaseName(objFile)

'objWord.ActiveDocument.Unprotect Password:="pwd" 

        objWord.ActiveDocument.Close(False)
    Next

    objWord.Quit
Else
    Msgbox("Run usning cmd")
End If
Sub UnprotectDocsInFolder()
Dim docfile As Document
Dim docpath As String
Dim docfilename As String
Dim pwd As String

    'Path for the documents
    docpath = "C:\ProtectedDocs\"
    'Password
    pwd = "myPass"

    docfilename = Dir(docpath & "*.doc")

    Do Until docfilename = ""
        Set docfile = Documents.Open(docpath & docfilename)
        docfile.Unprotect pwd
        docfile.Close True
        docfilename = Dir()
    Loop
End Sub

您可以使用类似的代码以相同的方式保护文件。

Sub ProtectDocsInFolder()
Dim docfile As Document
Dim docpath As String
Dim docfilename As String
Dim pwd As String

    'Path for the documents
    docpath = "C:\UnProtectedDocs\"
    'Password
    pwd = "myPass"

    docfilename = Dir(docpath & "*.doc")

    Do Until docfilename = ""
        Set docfile = Documents.Open(docpath & docfilename)
        docfile.Protect wdAllowOnlyFormFields, , pwd
        docfile.Close True
        docfilename = Dir()
    Loop
End Sub