ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 869|回复: 3

[求助] 关于批量解除excel保护密码的代码改造问题~!

[复制链接]

TA的精华主题

TA的得分主题

发表于 2022-11-28 16:37 | 显示全部楼层 |阅读模式
因工作需要,需要批量解除指定文件夹下的数百个excel,因为是不同部门提交上来的,保护密码多种多样,在论坛找到了合适单个文件解除的代码,但看着太复杂不知道怎么改造成“可选择文件夹”批量解密的版本,还请各位大佬赐教~!
  1. Public Sub 工作表解除保护()
  2. Const DBLSPACE As String = vbNewLine & vbNewLine
  3. Const AUTHORS As String = DBLSPACE & vbNewLine
  4. Const HEADER As String = "工作表保护密码"
  5. Const VERSION As String = DBLSPACE & ""
  6. Const REPBACK As String = DBLSPACE & ""
  7. Const ZHENGLI As String = DBLSPACE & " "
  8. Const ALLCLEAR As String = DBLSPACE & "该工作簿中的工作表密码保护已全部解除。" & DBLSPACE & "请记得重新设置密码" _
  9. & DBLSPACE & "注意:此方法仅用于遗忘密码使用。"
  10. Const MSGNOPWORDS1 As String = "该文件工作表中没有加密"
  11. Const MSGNOPWORDS2 As String = "该文件工作表中没有加密2"
  12. Const MSGTAKETIME As String = "请耐心等候!" & DBLSPACE & "按确定开始回复"
  13. Const MSGPWORDFOUND1 As String = "密码重新组合为:" & DBLSPACE & "$$" & DBLSPACE & _
  14. "如果该文件工作表有不同密码,将搜索下一组密码并修改清除"
  15. Const MSGPWORDFOUND2 As String = "密码重新组合为:" & DBLSPACE & "$$" & DBLSPACE & _
  16. "如果该文件工作表有不同密码,将搜索下一组密码并解除"
  17. Const MSGONLYONE As String = "确保为唯一的?"
  18. Dim w1 As Worksheet, w2 As Worksheet
  19. Dim i As Integer, j As Integer, k As Integer, l As Integer
  20. Dim m As Integer, n As Integer, i1 As Integer, i2 As Integer
  21. Dim i3 As Integer, i4 As Integer, i5 As Integer, i6 As Integer
  22. Dim PWord1 As String
  23. Dim ShTag As Boolean, WinTag As Boolean
  24. Application.ScreenUpdating = False
  25. With ActiveWorkbook
  26. WinTag = .ProtectStructure Or .ProtectWindows
  27. End With
  28. ShTag = False
  29. For Each w1 In Worksheets
  30. ShTag = ShTag Or w1.ProtectContents
  31. Next w1
  32. If Not ShTag And Not WinTag Then
  33. MsgBox MSGNOPWORDS1, vbInformation, HEADER
  34. Exit Sub
  35. End If
  36. MsgBox MSGTAKETIME, vbInformation, HEADER
  37. If Not WinTag Then
  38. Else
  39. On Error Resume Next
  40. Do 'dummy do loop
  41. For i = 65 To 66: For j = 65 To 66: For k = 65 To 66
  42. For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66
  43. For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66
  44. For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126
  45. With ActiveWorkbook
  46. .Unprotect Chr(i) & Chr(j) & Chr(k) & _
  47. Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & _
  48. Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
  49. If .ProtectStructure = False And _
  50. .ProtectWindows = False Then
  51. PWord1 = Chr(i) & Chr(j) & Chr(k) & Chr(l) & _
  52. Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
  53. Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
  54. MsgBox Application.Substitute(MSGPWORDFOUND1, _
  55. "$$", PWord1), vbInformation, HEADER
  56. Exit Do 'Bypass all for…nexts
  57. End If
  58. End With
  59. Next: Next: Next: Next: Next: Next
  60. Next: Next: Next: Next: Next: Next
  61. Loop Until True
  62. On Error GoTo 0
  63. End If

  64. If WinTag And Not ShTag Then
  65. MsgBox MSGONLYONE, vbInformation, HEADER
  66. Exit Sub
  67. End If
  68. On Error Resume Next

  69. For Each w1 In Worksheets
  70. 'Attempt clearance with PWord1
  71. w1.Unprotect PWord1
  72. Next w1
  73. On Error GoTo 0
  74. ShTag = False
  75. For Each w1 In Worksheets
  76. 'Checks for all clear ShTag triggered to 1 if not.
  77. ShTag = ShTag Or w1.ProtectContents
  78. Next w1
  79. If ShTag Then
  80. For Each w1 In Worksheets
  81. With w1
  82. If .ProtectContents Then
  83. On Error Resume Next
  84. Do 'Dummy do loop
  85. For i = 65 To 66: For j = 65 To 66: For k = 65 To 66
  86. For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66
  87. For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66
  88. For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126
  89. .Unprotect Chr(i) & Chr(j) & Chr(k) & _
  90. Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
  91. Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
  92. If Not .ProtectContents Then
  93. PWord1 = Chr(i) & Chr(j) & Chr(k) & Chr(l) & _
  94. Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & _
  95. Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)
  96. MsgBox Application.Substitute(MSGPWORDFOUND2, _
  97. "$$", PWord1), vbInformation, HEADER
  98. 'leverage finding Pword by trying on other sheets
  99. For Each w2 In Worksheets
  100. w2.Unprotect PWord1
  101. Next w2
  102. Exit Do 'Bypass all for…nexts
  103. End If
  104. Next: Next: Next: Next: Next: Next
  105. Next: Next: Next: Next: Next: Next
  106. Loop Until True
  107. On Error GoTo 0
  108. End If
  109. End With
  110. Next w1
  111. End If
  112. MsgBox ALLCLEAR, vbInformation, HEADER
  113. End Sub
复制代码


TA的精华主题

TA的得分主题

发表于 2022-11-28 17:28 | 显示全部楼层
Excel工作圈小工具V12.2.rar (167.54 KB, 下载次数: 23)


批量解除用这个

TA的精华主题

TA的得分主题

 楼主| 发表于 2022-11-29 09:09 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2022-11-29 11:50 | 显示全部楼层
Addison9827 发表于 2022-11-29 09:09
我用这个直接报错了

细看你的是破解工作表保护,这个工具也不支持批量
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-11-20 19:28 , Processed in 0.039110 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表