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

时间:2017-10-17 05:11:18

标签: vba ms-word wsh

我有多个word文档,需要在Developer模式下对它们施加限制。

我使用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

1 个答案:

答案 0 :(得分:1)

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