【问题标题】:Unprotect multiple word documents inside a folder using VBA使用 VBA 取消保护文件夹内的多个 word 文档
【发布时间】:2017-10-17 05:11:17
【问题描述】:

我有多个 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

【问题讨论】:

  • 它抛出什么错误,在哪一行?
  • 错误内容是什么?
  • Fil 只会给你文件名,而不是完整路径。试试Set objDoc = objWord.Documents.Open(objFSO..BuildPath(strFolder, Fil),,TRUE)

标签: vba ms-word wsh


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多