2017-10-17 126 views
0

我有多個Word文檔,這些文檔需要對開發人員模式施加的限制。使用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 
+0

它拋出什麼錯誤,以及在哪一行? –

+0

https://stackoverflow.com/questions/42194300/does-vbscript-allow-named-arguments-in-function-calls –

+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