使用VBA解除文件夹内的多个Word文档
问题描述:
我有多个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
答
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
它抛出什么错误,以及在哪一行? –
https://stackoverflow.com/questions/42194300/does-vbscript-allow-named-arguments-in-function-calls –
错误的文字是什么? –