在我对original question here做出回答之前,但是现在看来该方法不起作用,而且我不确定为什么,因为我以为我在得到答案之后就可以使用它。
我有一个受密码保护的Excel文件,当我打开该文件时要求输入密码。我提供了密码,然后运行我的宏,该宏应该在没有密码的情况下保存,但是失败了。
宏运行并似乎可以正常工作后,我去重新打开文件,它仍然提示我输入密码。
知道为什么会这样吗?
请记住,密码是在SaveAs期间设置的,如下所示:
ActiveWorkbook.SaveAs fileName:=.SelectedItems(1), Password:=pw_check, FileFormat:=file_format
解密代码:
Sub SimpleRemovePassword()
Dim active_wb As Workbook
Dim file_format As Variant
Dim fname As String
Dim fd As Office.FileDialog
Set active_wb = ActiveWorkbook
fname = active_wb.Name
file_format = active_wb.FileFormat
' Verify with user before continuing '
If ActiveWorkbook.HasPassword Then
Application.DisplayAlerts = False
If MsgBox("You are about to remove password encryption from this file and save. Would you like to continue?", vbYesNo) = vbNo Then Exit Sub
active_wb.Unprotect
active_wb.SaveAs fileName:=fname, Password:="", FileFormat:=file_format
Application.DisplayAlerts = True
Else
MsgBox ("This workbook does not have a password.")
End If
End Sub