VBA清除Excel密码保护,2003/2007/2010均适用

Sub Macro1()
'
' Breaks worksheet and workbook structure passwords. Jason S

' probably originator of base code algorithm modified for coverage

' of workbook structure / windows passwords and for multiple passwords

' Jason S http://jsbi.blogspot.com

' Reveals hashed passwords NOT original passwords

Const DBLSPACE As String = vbNewLine & vbNewLine

Const AUTHORS As String = DBLSPACE & vbNewLine & "Adapted from Bob McCormick base code by" & "Jason S http://jsbi.blogspot.com"

Const HEADER As String = "AllInternalPasswords User Message"

Const VERSION As String = DBLSPACE & "Version 1.0 8 Sep 2008"

Const REPBACK As String = DBLSPACE & "Please report failure to [email protected] "

Const ALLCLEAR As String = DBLSPACE & "The workbook should be cleared"

Const MSGNOPWORDS1 As String = "There were no passwords on " & AUTHORS & VERSION

Const MSGNOPWORDS2 As String = "There was no protection to " & "workbook structure or windows." & DBLSPACE

Const MSGTAKETIME As String = "After pressing OK button this " & "will take some time." & DBLSPACE & "Amount of time " & "depends on how many different passwords, the "

Const MSGPWORDFOUND1 As String = "You had a Worksheet " & "Structure or Windows Password set." & DBLSPACE & "The password found was: " & DBLSPACE & "$$" & DBLSPACE & "Note it down for potential future use in other workbooks by " & "the same person who set this password." & DBLSPACE & "Now to check and clear other passwords." & AUTHORS & VERSION

Const MSGPWORDFOUND2 As String = "You had a Worksheet " & "password set." & DBLSPACE & "The password found was: " & DBLSPACE & "$$" & DBLSPACE & "Note it down for potential " & "future use in other workbooks by same person who " & "set this password." & DBLSPACE & "Now to check and clear " & "other passwords." & AUTHORS & VERSION

Const MSGONLYONE As String = "Only structure / windows " & "protected with the password that was just found." & ALLCLEAR & AUTHORS & VERSION & REPBACK

Dim w1 As Worksheet, w2 As Worksheet

Dim i As Integer, j As Integer, k As Integer, l As Integer

Dim m As Integer, n As Integer, i1 As Integer, i2 As Integer

Dim i3 As Integer, i4 As Integer, i5 As Integer, i6 As Integer

Dim PWord1 As String

Dim ShTag As Boolean, WinTag As Boolean

Application.ScreenUpdating = False

With ActiveWorkbook

WinTag = .ProtectStructure Or .ProtectWindows

End With

ShTag = False

For Each w1 In Worksheets

ShTag = ShTag Or w1.ProtectContents

Next w1

If Not ShTag And Not WinTag Then

MsgBox MSGNOPWORDS1, vbInformation, HEADER

Exit Sub

End If

MsgBox MSGTAKETIME, vbInformation, HEADER

If Not WinTag Then

MsgBox MSGNOPWORDS2, vbInformation, HEADER

Else

On Error Resume Next

Do 'dummy do loop

For i = 65 To 66: For j = 65 To 66: For k = 65 To 66

For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66

For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66

For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126

With ActiveWorkbook

.Unprotect Chr(i) & Chr(j) & Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)

If .ProtectStructure = False And .ProtectWindows = False Then

PWord1 = Chr(i) & Chr(j) & Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)

MsgBox Application.Substitute(MSGPWORDFOUND1, "$$", PWord1), vbInformation, HEADER

Exit Do 'Bypass all for...nexts

End If

End With

Next: Next: Next: Next: Next: Next

Next: Next: Next: Next: Next: Next

Loop Until True

On Error GoTo 0

End If

If WinTag And Not ShTag Then

MsgBox MSGONLYONE, vbInformation, HEADER

Exit Sub

End If

On Error Resume Next

For Each w1 In Worksheets

'Attempt clearance with PWord1

w1.Unprotect PWord1

Next w1

On Error GoTo 0

ShTag = False

For Each w1 In Worksheets

'Checks for all clear ShTag triggered to 1 if not.

ShTag = ShTag Or w1.ProtectContents

Next w1

If ShTag Then

For Each w1 In Worksheets

With w1

If .ProtectContents Then

On Error Resume Next

Do 'Dummy do loop

For i = 65 To 66: For j = 65 To 66: For k = 65 To 66

For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66

For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66

For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126

.Unprotect Chr(i) & Chr(j) & Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)

If Not .ProtectContents Then

PWord1 = Chr(i) & Chr(j) & Chr(k) & Chr(l) & Chr(m) & Chr(i1) & Chr(i2) & Chr(i3) & Chr(i4) & Chr(i5) & Chr(i6) & Chr(n)

MsgBox Application.Substitute(MSGPWORDFOUND2, "$$", PWord1), vbInformation, HEADER

'leverage finding Pword by trying on other sheets

For Each w2 In Worksheets

w2.Unprotect PWord1

Next w2

Exit Do 'Bypass all for...nexts

End If

Next: Next: Next: Next: Next: Next

Next: Next: Next: Next: Next: Next

Loop Until True

On Error GoTo 0

End If

End With

Next w1

End If

MsgBox ALLCLEAR & AUTHORS & VERSION & REPBACK, vbInformation, HEADER
'
End Sub

VBA清除Excel密码保护,2003/2007/2010均适用

时间: 2024-11-05 14:49:25

VBA清除Excel密码保护,2003/2007/2010均适用的相关文章

Office 2003 2007 2010 配置进度 正在配置 解决方案 (转载)

在安装过Office2003.2007 或者2010之后,如果没有选择全部的组件,或者是因为安装到非系统盘,有时候打开 Office 文档的时候就会出现正在配置Office,或者Office配置进度的对话框,虽然总不会影响使用,但每次打开之后都会出现,是很浪费时间的一件事,对于我们这些视时间如金钱的人来说,当然不能容忍它再次发生了.所以现在给出解决办法. 如果出现这种情况,就 “开始”--“运行”对话框里输入 “cmd”--回车(如果是Vista 或者 Windows 7,则右键--管理员身份打

用VBA取得EXCEL有效行列数方法研究

作者:iamlaosong 我常用下面方式取得有效行数: maxrow=sheets(1).[A65536].End(xlUp).Row 实际使用中发现这种方法存在2个问题: 1.高低版本不兼容,2007版最大行数增加以后,就不能用65536了,而要用1048576,即 maxrow = sheets(1).[A1048576].End(xlUp).Row 2.当最后一行不为空时,用这个方法取不到正确的值. 下面说明用VBA得到EXCEL表格中的行数和列数的特点,以便根据需要选用 每种方法中上面

【VBA研究】用VBA取得EXCEL随意列有效行数

作者:iamlaosong 用VBA对Excel文件进行处理的时候,keyword段的列号编程时往往是不知道的.须要通过參数设定才干知道,因此.我们编程的时候,就不能用这种语句取有效行数: lineno = [B65536].End(xlUp).Row          '从下至上找有效行数 上述语句中的列名"B"假设是变量.能够用字符串连接的方式实现,即: pos_ems = "C" lineno = Range(pos_ems & "65536

VBA取得EXCEL表格中的行数和列数

VBA取得EXCEL表格中的行数和列数 初学EXCEL宏的童鞋,总是很想知道表格中含有数据的行数和列数,尤其是行数和列数不确定的情况下.这样可以避免很多的错误,并且可以提高效率.但每次用到的时候到网上查找时,总是给了很多无用的答案,往往找不到想要的结果.笔者也是每次使用时,临时查找总是很头疼.偶然发现一篇博客,上面详细记录了不同的方法,笔者测试了几种发现真的很好用.本着分享万岁的精神,将博客内容共享出来.希望对大家有所帮助. 来源:http://www.okexcel.com.cn/bbs/vi

[SharePoint 2010]Excel Services in SharePoint 2010 REST API 使用语法

摘要:Excel Services in SharePoint 2010 REST API 使用语法 在之前 SharePoint 2007 若要在Web 上呈现Excel文件只能透过 Excel Services 方式, 现在在SharePoint 2010 除了原本Excel Services 的功能, 它更提供了 Excel Services REST API 透过 URI (Uniform Resource Identifier) 将数据显示在Web browser. 下面是将一个在Sh

VBA在Excel中的应用(一):改变符合条件单元格的背景颜色

在使用excel处理数据的时候,为了能更清晰的标示出满足特定条件的单元格,对单元格添加背景色是不错的选择.手工处理的方式简单快捷,但是当遇到大批量数据,就会特别的费时费力,而且不讨好(容易出错).通过代码来处理是个不错的选择,excel可以通过VBA编程来处理内部数据,在打开excel页面后,可以通过“alt + F11”组合键来启动VBA编程界面,跟VB的编程界面和语法一样,需要注意的是如何调用excel的内容.VBA通过sheet, range和cells三个层次来调用excel中的制定区域

利用VBA遍历Excel所有sheet的例子

下面是利用VBA遍历Excel sheet的例子,可以用于Excel的批量处理. Sub Insert_CodeString() Dim fs, ft As Object Dim xlApp As Excel.Application Dim xlBook As Excel.Workbook Dim xlSheet As Excel.Worksheet Dim Sql As String Sql = "truncate table code_string;" & Chr(10)

excel中vba将excel中数字和图表输出到word中

参考:https://wenku.baidu.com/view/6c60420ecc175527072208af.html 比如将选区变为图片保存到桌面: 1 Sub 将选区转为图片存到桌面() 2 Dim ans As Byte, Pic As String, Paths As String 3 On Error Resume Next 4 Paths = CreateObject("WScript.Shell").SpecialFolders("Desktop"

VBA操作Excel

几个小知识: 1.设置单元格格式: sht.Cells(4, pCol).NumberFormatLocal = "yyyy-mm-dd hh:mm:ss" 其中文本格式为:"@",常规格式为:G/通用格式 2.设置单元格为自动列宽或自动高度: #1.VBA自动生成的代码为:Columns("A:D").EntireColumn.AutoFit #2.但是字母的A到D,我们在VBA中不方便取到,一般都是数据,所以使用 Range(Cells(1,