我无法使用 VBA 解锁 vbaproject

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11305192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:42:52  来源:igfitidea点击:

I cant unlock a vbaproject with VBA

vbaexcel-vbaexcel

提问by Merni

Down below is my function, for some reason it does not work. If I place a breakpoint in the last if statement and removes sending "%{F11}" it does work. So my guess is that "%F11" is not working. Does anyone have an idea?

下面是我的功能,由于某种原因它不起作用。如果我在最后一个 if 语句中放置一个断点并删除发送“%{F11}”,它确实有效。所以我的猜测是“%F11”不起作用。有没有人有想法?

Sub UnprotecPassword(wb As Workbook, ByVal projectPassword As String)
    Dim currentActiveWb As Workbook    

    If wb.VBProject.Protection <> vbext_pp_locked Then
        Exit Sub
    End If
    Set currentActiveWb = ActiveWorkbook
    wb.Activate

    SendKeys "%{F11}"
    SendKeys "^r" ' Set focus to Explorer
    SendKeys "{TAB}" ' Tab to locked project
    SendKeys "~" ' Enter
    SendKeys projectPassword
    SendKeys "~" ' Enter

    If (wb.VBProject.Protection = vbext_pp_locked) Then
        MsgBox ("failed to unlock")
    End If
    currentActiveWb.Activate
End Sub

回答by Siddharth Rout

To test this, Let's create a new workbook called Book2.xlsm.

为了测试这一点,让我们创建一个名为Book2.xlsm.

for testing purpose paste this code in the Book2 Module.

出于测试目的,将此代码粘贴到 Book2 模块中。

Sub Book2Macro()

End Sub

Protect it with a password say aand then close it. This is necessary for the Locking to take effect.

用密码保护它,a然后关闭它。这是锁定生效所必需的。

Now create a new workbook say Book1 and in the module paste this code.

现在创建一个新的工作簿 Book1 并在模块中粘贴此代码。

Sub Sample()
    UnprotecPassword Workbooks("Book2.xlsm"), "a"
End Sub

Sub UnprotecPassword(wb As Workbook, ByVal projectPassword As String)
    Dim currentActiveWb As Workbook

    If wb.VBProject.Protection <> 1 Then
        Exit Sub
    End If

    Set currentActiveWb = ActiveWorkbook

    wb.Activate

    SendKeys "%{F11}"
    SendKeys "^r" ' Set focus to Explorer
    SendKeys "{TAB}" ' Tab to locked project
    SendKeys "~" ' Enter
    SendKeys projectPassword
    SendKeys "~" ' Enter

    If (wb.VBProject.Protection = vbext_pp_locked) Then
        MsgBox ("failed to unlock")
    End If
    currentActiveWb.Activate
End Sub

Now open the 1st workbook that we created; Book2.xlsm. Check the VBA Editor for Book2 and you will notice that it is password protected. You will also notice that it is the active workbook. Activate Book1by clicking the View Tab | Switch Workbooks | Book1

现在打开我们创建的第一个工作簿;Book2.xlsm。检查 Book2 的 VBA 编辑器,您会注意到它受密码保护。您还会注意到它是活动工作簿。Book1通过单击激活View Tab | Switch Workbooks | Book1

enter image description here

在此处输入图片说明

Now click on Developer tab | MacrosIf you can't see Developer tab then I would recommend going through this link.

现在单击Developer tab | Macros如果您看不到开发人员选项卡,那么我建议您浏览此链接

enter image description here

在此处输入图片说明

Click on the the SampleMacro in the Macro Dialog Boxand you are done.

单击中的SampleMacro Dialog Box,您就完成了。

enter image description here

在此处输入图片说明

If you check the VBA Editor, you will notice that the VBA Editor for Book2 is now unlocked/accessible.

如果您检查 VBA 编辑器,您会注意到 Book2 的 VBA 编辑器现在已解锁/可访问。

Sendkeys are unreliable depending on your use of it. If you use it correctly then that are pretty much reliable :)

Sendkeys 是不可靠的,这取决于您对它的使用。如果您正确使用它,那么这非常可靠:)

There is one more way to unlock the VBA Password but that is pretty complex and involves invoking the API like FindWindow etc...

还有另一种解锁 VBA 密码的方法,但这非常复杂,涉及调用诸如 FindWindow 等 API...

回答by Trace

Check out these posts for code samples:
http://www.mrexcel.com/archive/VBA/29825.html
http://www.vbaexpress.com/forum/showthread.php?t=30687

查看这些帖子以获取代码示例:
http: //www.mrexcel.com/archive/VBA/29825.html
http://www.vbaexpress.com/forum/showthread.php?t=30687

And these posts are for info:
http://www.excelforum.com/excel-programming/490883-why-doesnt-sendkeys-work-consistently.html
http://www.ozgrid.com/forum/showthread.php?t=13006

这些帖子仅供参考:
http: //www.excelforum.com/excel-programming/490883-why-doesnt-sendkeys-work-consistently.html
http://www.ozgrid.com/forum/showthread.php? t=13006

They discuss why using Sendkeys is not very reliable in a multitasking environment and many discourage the use for commercial purpose. However, for unprotecting VBA projects, it appears to be the only solution.

他们讨论了为什么在多任务环境中使用 Sendkeys 不是很可靠,并且许多人不鼓励将其用于商业目的。但是,对于取消保护 VBA 项目,它似乎是唯一的解决方案。

Hope it helps!

希望能帮助到你!