0

I have multiple word documents, which need restrictions imposed on them from Developer mode.

I run the script using wscript passing a folder as argument, But it throes an error

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
4
  • What error does it throw, and on which line? Commented Oct 17, 2017 at 5:28
  • stackoverflow.com/questions/42194300/… Commented Oct 17, 2017 at 5:30
  • What is the text of the error? Commented Oct 17, 2017 at 6:30
  • Fil will just give you the name of the file, not the full path.. Try Set objDoc = objWord.Documents.Open(objFSO..BuildPath(strFolder, Fil),,TRUE) Commented Oct 17, 2017 at 6:33

1 Answer 1

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

You can use similar code to protect files in same manner.

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.