我可以在打开的工作簿上编译 VBA 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28001620/
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
Can I compile VBA on workbook open?
提问by Examorph
I am trying to find a way of compiling the code in VBA on workbook open. I can do this manually by opening the VBA environment, going into Debug and "Compile VBAProject" but was wondering if there is a way to do this through the code every time the workbook opens.
我试图找到一种在工作簿打开时在 VBA 中编译代码的方法。我可以通过打开 VBA 环境、进入调试和“编译 VBAProject”来手动执行此操作,但想知道是否有办法在每次打开工作簿时通过代码执行此操作。
The purpose of this is to load the code into the computers memory to prevent a compile error due to use of some Active X Objects. As mentioned, I can do this manually and it solves the problem however, as there are many users that use this and the workbook is password protected, not all will have access to the debug option.
这样做的目的是将代码加载到计算机内存中,以防止由于使用某些 Active X 对象而导致编译错误。如前所述,我可以手动执行此操作,但它解决了问题,因为有许多用户使用它并且工作簿受密码保护,并非所有人都可以访问调试选项。
回答by Matteo NNZ
I think you might try to do this by automating the VBE (Visual Basic Editor).
我认为您可能会尝试通过自动化 VBE(Visual Basic 编辑器)来做到这一点。
REQUIREMENT:
要求:
you need to go to Excel / File / Options / Trust Center / Trust Center settings
and check the option Trust access to the VBA project object model
(for security reasons this is deactivated by default, and if you don't check it the below code will raise the run-time error 1004 programmatic access to visual basic project is not trusted
). Clearly, you only need to do this once (in each computer you want to execute the automated compilation, of course).
您需要去Excel / File / Options / Trust Center / Trust Center settings
检查该选项Trust access to the VBA project object model
(出于安全原因,默认情况下这是禁用的,如果您不检查它,下面的代码将引发运行时错误 1004 programmatic access to visual basic project is not trusted
)。显然,您只需要执行一次(当然,在您要执行自动编译的每台计算机中)。
CODING:
编码:
Your command bar instruction (i.e. "Compile VBA Project") is inside the VBE object of the Excel Application, specifically in the command bars:
您的命令栏指令(即“编译 VBA 项目”)位于 Excel 应用程序的 VBE 对象内,特别是在命令栏中:
Dim objVBECommandBar As Object
Set objVBECommandBar = Application.VBE.CommandBars
The object will now contain the entire command bar of the Visual Basic Editor. In particular, you look for the ID button "578", which is in fact the "Compile VBA Project" (you can put a watcher on the variable and browse all is inside into the local window, you might want to search for other commands). Hence, to summarize:
该对象现在将包含 Visual Basic 编辑器的整个命令栏。特别是,您寻找 ID 按钮“578”,它实际上是“编译 VBA 项目”(您可以在变量上放置一个观察者并在本地窗口中浏览所有内容,您可能想搜索其他命令)。因此,总结一下:
Set compileMe = objVBECommandBar.FindControl(Type:=msoControlButton, ID:=578)
compileMe.Execute
This will allow the compilation of the project. As you were asking, you just put this into the This Workbook open event:
这将允许编译项目。正如您所问的,您只需将其放入“本工作簿”开放事件中:
Private Sub ThisWorkbook_Open()
Set compileMe = objVBECommandBar.FindControl(Type:=msoControlButton, ID:=578)
compileMe.Execute 'the project should hence be compiled
End Sub