|
这两段代码,是在论坛上找到的(出处:守柔版主)
批量添加,解除word打开密码。
如何修改才能让这两段代码,在excel中能够运行?
要引用word对象,这点我添加了,也是报错,调试了很久,没有成功,故发贴求解,谢谢。
Sub SAMPLE()
'批量添加打开文档密码
Dim myDialog As FileDialog, oFile As Variant, oDoc As Document
Dim NewFolder As String
On Error Resume Next
NewFolder = "F:\TEMP\"
'定义一个文件夹选取对话框
Set myDialog = Application.FileDialog(msoFileDialogFilePicker)
With myDialog
.Filters.Clear '清除所有文件筛选器中的项目
.Filters.Add "所有 WORD 文件", "*.doc", 1 '增加筛选器的项目为所有WORD文件
.AllowMultiSelect = True '允许多项选择
If .Show <> -1 Then Exit Sub
For Each oFile In .SelectedItems '在所有选取项目中循环
Set oDoc = Documents.Open(FileName:=oFile, Visible:=False)
With oDoc
.Password = "你的密码"
.Close True '关闭文档
End With
Next oFile
End With
End Sub
Sub Example()
'批量解除打开文档密码
Dim myDialog As FileDialog, oFile As Variant, oDoc As Document
Dim myPassWord As String
' On Error Resume Next
myPassWord = "tangqingfu"
'定义一个文件夹选取对话框
Set myDialog = Application.FileDialog(msoFileDialogFilePicker)
With myDialog
.Filters.Clear '清除所有文件筛选器中的项目
.Filters.Add "所有 WORD 文件", "*.doc", 1 '增加筛选器的项目为所有WORD文件
.AllowMultiSelect = True '允许多项选择
If .Show <> -1 Then Exit Sub
For Each oFile In .SelectedItems '在所有选取项目中循环
Set oDoc = Documents.Open(FileName:=oFile, Visible:=False, PasswordDocument:=myPassWord)
With oDoc
.ReadOnlyRecommended = False '请勿省略
.Password = ""
.Save
.Close '关闭文档
End With
Next oFile
End With
End Sub
。
|
|