|
谢谢kqbt兄的代码。
楼主也可以参考此帖中守版提供的代码
http://club.excelhome.net/viewth ... ngqingfu&page=1- Sub 批量设置和解除指定文件夹下文档的打开文件时的密码()
- Dim myDialog As FileDialog, oFile As Variant, oDoc As Document
- Dim myPassWord As String
- Dim OldPW As String
- Dim myVar As Variable
- Dim aVar As Variable
- Dim Num As Integer
- On Error Resume Next
- '定义一个文件夹选取对话框
- Set myDialog = Application.FileDialog(msoFileDialogFilePicker)
- With myDialog
- .Filters.Clear '清除所有文件筛选器中的项目
- .Filters.Add "所有 WORD 文件", "*.doc", 1 '增加筛选器的项目为所有WORD文件
- .AllowMultiSelect = True '允许多项选择
- If .Show <> -1 Then Exit Sub
- With ThisDocument
- For Each aVar In .Variables
- If aVar.name = "PW" Then Set myVar = aVar: Exit For
- Next
- If myVar Is Nothing Then
- Set myVar = .Variables.Add(name:="PW", Value:="EMPTY")
- End If
- OldPW = myVar.Value
- If OldPW = "EMPTY" Then OldPW = ""
- myPassWord = VBA.InputBox("请输入需要设置的文档保护密码,如果什么也不输入或者按取消则视为解除打开密码!")
- If myPassWord = "" Then
- .Variables("PW").Value = "EMPTY"
- Else
- .Variables("PW").Value = myPassWord
- End If
- .Save
- End With
- For Each oFile In .SelectedItems '在所有选取项目中循环
- Set oDoc = Documents.Open(FileName:=oFile, Visible:=False, PasswordDocument:=OldPW)
- With oDoc
- .ReadOnlyRecommended = False '请勿省略
- .Password = myPassWord
- .Close True '关闭文档
- End With
- Next oFile
- End With
- End Sub
复制代码
[ 本帖最后由 tangqingfu 于 2009-12-8 00:21 编辑 ] |
|