| |
| |
| Const sTitle = "VBS 批量设置 Office Word,Excel 写保护工具 By Yu2n@qq.com" |
| Call CommandMode(sTitle) |
| Call Main() |
| |
| Sub Main() |
| |
| |
| nChoice = Msgbox("请选择:" & vbCrLf & vbCrLf & _ |
| vbTab & "[ 是(Y) ] 新增写保护密码 (修改密码请先删除后新增)" & vbTab & vbCrLf & vbCrLf & _ |
| vbTab & "[ 否(N) ] 删除写保护密码" & vbCrLf & vbCrLf & _ |
| vbTab & "[ 取消 ] 退出程序" & vbCrLf _ |
| , vbInformation+vbYesNoCancel, sTitle) |
| Select Case nChoice |
| Case vbYes : bProtect = True |
| Case vbNo : bProtect = False |
| Case Else |
| WScript.Quit |
| End Select |
| |
| |
| sPassword = Inputbox( "> 请输入写保护密码:", sTitle, "" ) |
| |
| |
| Dim strFolder, arrPath, strPath, nFileCount, i |
| WScript.Echo " --- 请选择 Office Word,Excel 文件路径:" |
| strFolder = BrowseForFolder("请选择 Office Word,Excel 文件路径:") |
| WScript.Echo strFolder & vbCrLf |
| If strFolder = "" Then Exit Sub |
| arrPath = ScanFolder(strFolder) |
| For Each strPath In arrPath |
| If InStr(1, "|.doc|.xls|", Right(strPath,4), vbTextCompare) > 0 Or _ |
| InStr(1, "|.docx|.xlsx|", Right(strPath,5), vbTextCompare) > 0 Then |
| nFileCount = nFileCount + 1 |
| End If |
| Next |
| |
| |
| Dim dtStart, objWord, objExcel, objPowerPoint |
| dtStart = Now() |
| WScript.Echo " --- 正在启动 Office Word,Excel ... " & vbCrLf |
| Call Word_Init(objWord, objExcel, objPowerPoint) |
| For Each strPath In arrPath |
| If InStr(1, "|.doc|.xls|", Right(strPath,4), vbTextCompare) > 0 Or _ |
| InStr(1, "|.docx|.xlsx|", Right(strPath,5), vbTextCompare) > 0 Then |
| |
| |
| i = i + 1 |
| strFN = Right(strPath,Len(strPath)-InstrRev(strPath,"\")) |
| strMsg = "[成功]" |
| |
| |
| If LCase(Right(strPath,4))=".doc" Or LCase(Right(strPath,5))=".docx" Then _ |
| If Not WordProtect(objWord, strPath, sPassword, bProtect) Then _ |
| strMsg = "[失败]" |
| If LCase(Right(strPath,4))=".xls" Or LCase(Right(strPath,5))=".xlsx" Then _ |
| If Not ExcelProtect(objExcel, strPath, sPassword, bProtect) Then _ |
| strMsg = "[失败]" |
| |
| WScript.Echo "[" & i & "/" & nFileCount & "] " & strFN & vbTab & strMsg |
| End If |
| Next |
| |
| |
| WScript.Echo vbCrLf & " --- 正在退出 Office Word,Excel ... " & vbCrLf |
| objWord.Quit |
| objExcel.Quit |
| WScript.Echo " --- 完成。耗时 " & DateDiff("s",dtStart,Now()) & " 秒。" & vbCrLf |
| Msgbox "总计 " & nFileCount & " 个文档完成设置,耗时 " & DateDiff("s",dtStart,Now()) & " 秒。", _ |
| vbInformation+vbOKOnly, sTitle |
| End Sub |
| |
| |
| |
| Sub Word_Init(ByRef objWord, ByRef objExcel, ByRef objPowerPoint) |
| On Error Resume Next |
| Const msoAutomationSecurityForceDisable = 3 |
| Set objWord = CreateObject("Word.Application") |
| Set objExcel = CreateObject("Excel.Application") |
| If Not Err.Number = 0 Then |
| Msgbox "错误:无法创建 Office VBA 对象,请安装 Office Word,Excel ...", _ |
| vbCritical+vbOKOnly, sTitle |
| WScript.Quit(999) |
| End If |
| If Not objWord.Application.Version >= 12.0 Then |
| Msgbox "警告:请使用 Office 2007 以上版本。", vbExclamation+vbOKOnly, sTitle |
| End If |
| |
| objWord.Visible = False : objWord.DisplayAlerts = False |
| objExcel.Visible = False : objExcel.DisplayAlerts = False |
| |
| |
| objWord.AutomationSecurity = msoAutomationSecurityForceDisable |
| objExcel.AutomationSecurity = msoAutomationSecurityForceDisable |
| End Sub |
| |
| |
| Function WordProtect(ByRef objWord, ByVal sFilePath, ByVal sPassword, ByVal bProtect) |
| On Error Resume Next |
| Const wdAllowOnlyReading = 3 |
| WordProtect = False |
| If Not CreateObject("Scripting.Filesystemobject").FileExists(sFilePath) Then Exit Function |
| Set objDoc = objWord.Documents.Open(sFilePath) |
| If Not objDoc.ProtectionType = wdAllowOnlyReading Then |
| If bProtect Then |
| Err.Clear |
| objDoc.Protect wdAllowOnlyReading, Ture, CStr(sPassword) |
| If Err.Number = 0 Then WordProtect = True |
| End If |
| Else |
| If Not bProtect Then objDoc.Unprotect sPassword |
| If Not objDoc.ProtectionType = wdAllowOnlyReading Then WordProtect = True |
| End If |
| objDoc.Save |
| objDoc.Close False |
| |
| End Function |
| |
| |
| Function ExcelProtect(ByRef objExcel, ByVal sFilePath, ByVal sPassword, ByVal bProtect) |
| On Error Resume Next |
| Const wdAllowOnlyReading = 3 |
| ExcelProtect = False |
| If Not CreateObject("Scripting.Filesystemobject").FileExists(sFilePath) Then Exit Function |
| Set objWorkbook = objExcel.WorkBooks.Open(sFilePath) |
| For Each objWorkSheet In objWorkbook.Worksheets |
| If objWorkSheet.ProtectContents = False Then |
| If bProtect Then |
| Err.Clear |
| objWorkSheet.Protect CStr(sPassword) |
| If Err.Number = 0 Then ExcelProtect = True |
| End If |
| Else |
| If Not bProtect Then objWorkSheet.Unprotect CStr(sPassword) |
| If objWorkSheet.ProtectContents = False Then ExcelProtect = True |
| End If |
| Next |
| objWorkbook.Save |
| objWorkbook.Close False |
| |
| End Function |
| |
| |
| Function BrowseForFolder(ByVal strTips) |
| Dim objFolder |
| Set objFolder = CreateObject("Shell.Application").BrowseForFolder (&H0, strTips, &H0010 + &H0001) |
| If (Not objFolder Is Nothing) Then BrowseForFolder = objFolder.Self.Path |
| End Function |
| |
| |
| Function ScanFolder(ByVal strPath) |
| Dim arr() : ReDim Preserve arr(0) : arr(0) = strPath |
| Call SCAN_FOLDER(arr, strPath) : ScanFolder = arr |
| End Function |
| Function SCAN_FOLDER(ByRef arr, ByVal folderSpec) |
| On Error Resume Next |
| Dim fso, objItems, objFile, objFolder |
| Set fso = CreateObject("Scripting.FileSystemObject") |
| Set objItems = fso.GetFolder(folderSpec) |
| If Right(folderSpec, 1) <> "\" Then folderSpec = folderSpec & "\" |
| If (Not fso.FolderExists(folderSpec)) Then Exit Function |
| For Each objFile In objItems.Files |
| ReDim Preserve arr(UBound(arr) + 1) |
| arr(UBound(arr)) = objFile.Path |
| Next |
| For Each objFolder In objItems.subfolders |
| ReDim Preserve arr(UBound(arr) + 1) |
| arr(UBound(arr)) = objFolder.Path |
| Call SCAN_FOLDER(arr, objFolder.Path) |
| Next |
| End Function |
| |
| |
| Sub CommandMode(ByVal sTitle) |
| If InStr(1, WScript.FullName, "\cscript.exe", vbTextCompare) > 0 Then Exit Sub |
| sCommand = "%Comspec% /c title " & sTitle & " & cscript.exe //NoLogo """ & WScript.ScriptFullName & """" |
| For Each oArg In WScript.Arguments |
| sArgs = sArgs & " " & """" & oArg & """" |
| Next |
| CreateObject("WScript.Shell").Run sCommand & sArgs, 1, False |
| WScript.Quit |
| End SubCOPY |