简体   繁体   English

Workbook_Open() 不适用于工作簿保护

[英]Workbook_Open() does not work with Workbook Protection

I am using Excel 2016 and have this code written in the ThisWorkbook object in VBA:我正在使用 Excel 2016 并将此代码写入 VBA 中的 ThisWorkbook object:

Private Sub Workbook_Open()
ThisWorkbook.Protect (password = "password")
End Sub

It is not working.它不工作。 The point here is that this should prevent users from touching the Power Query functions in this workbook.这里的要点是,这应该可以防止用户接触本工作簿中的 Power Query 函数。 I have saved it as a Macro-enabled workbook, I have all macros and events enabled and this is the only workbook open.我已将其保存为启用宏的工作簿,我启用了所有宏和事件,这是唯一打开的工作簿。

I also have this additional code:我还有这个附加代码:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
ThisWorkbook.Protect (password = "password")
ThisWorkbook.Save
End Sub

And that is not working either.那也行不通。 It works fine if I insert that "ThisWorkbook.Protect" code into a general module or worksheet object and run it manually, but when I want this particular excel file to run this code automatically on open or close, it does not do it.如果我将该“ThisWorkbook.Protect”代码插入通用模块或工作表 object 并手动运行它,它工作正常,但是当我希望这个特定的 excel 文件在打开或关闭时自动运行此代码时,它不会这样做。

Any ideas what could be causing this?任何想法可能是什么原因造成的?

For some reason running ThisWorkbook.Protect on a protected workbook seems to unprotect it (even though I couldn't find any documentation that says that it does this) so try this:出于某种原因,在受保护的工作簿上运行ThisWorkbook.Protect似乎取消了它的保护(即使我找不到任何说明它这样做的文档)所以试试这个:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If Not ThisWorkbook.ProtectStructure Then ThisWorkbook.Protect Password:="password"
    ThisWorkbook.Save
End Sub

In order for automatically-running subroutines to work correctly in Microsoft Excel, they must be contained within a Visual Basic module.为了使自动运行的子例程在 Microsoft Excel 中正常工作,它们必须包含在 Visual Basic 模块中。

If an Auto_Open, Auto_Close, or other automatically-running subroutine is stored "behind" a worksheet or ThisWorkbook, it may not function correctly when you open or close your workbook, or when you perform an action that should cause the subroutine to run.如果 Auto_Open、Auto_Close 或其他自动运行的子例程存储在工作表或 ThisWorkbook“后面”,则当您打开或关闭工作簿时,或者当您执行应导致子例程运行的操作时,它可能无法正确 function 。

https://support.microsoft.com/en-us/topic/vba-code-behind-a-worksheet-or-a-workbook-may-not-work-in-excel-f2de64d3-a926-7035-e18e-1697f0a32bc5#:~:text=In%20order%20for%20automatically%2Drunning%20subroutines%20to%20work%20correctly%20in%20Microsoft%20Excel%2C%20they%20must%20be%20contained%20within%20a%20Visual%20Basic%20module . https://support.microsoft.com/en-us/topic/vba-code-behind-a-worksheet-or-a-workbook-may-not-work-in-excel-f2de64d3-a926-7035-e18e- 1697f0a32bc5#:~:text=In%20order%20for%20automatically%2Drunning%20subroutines%20to%20work%20correctly%20in%20Microsoft%20Excel%2C%20they%20must%20be%20contained%20within%20a%20Visual%20Basic%20module .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM