VBA 代码可以在 MS 应用程序之外运行吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8171214/
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 VBA Code Be Run Outside of MS Applications?
提问by Roman
I just started to learn VBA. As far as I understood, one uses VBA to write macros in MS-Excel. But I wonder if it the only area of application for VBA. Can it be also used like many other standard languages - like Python, for example.
我刚开始学习VBA。据我了解,有人使用 VBA 在 MS-Excel 中编写宏。但我想知道它是否是 VBA 的唯一应用领域。它也可以像许多其他标准语言一样使用 - 例如 Python。
If it is the case, how can I compile a VBA program? Should it be compiled? How can I run such a VBA program?
如果是这样,我该如何编译 VBA 程序?应该编译吗?如何运行这样的 VBA 程序?
回答by Justin Self
VBA is compiled to p-code. P-code is an intermediate language that requires an additional runtime in order to execute. This runtime is hosted by most Microsoft Office applications (including various non-Microsoft applications).
VBA 被编译为 p 代码。P 代码是一种中间语言,需要额外的运行时才能执行。此运行时由大多数 Microsoft Office 应用程序(包括各种非 Microsoft 应用程序)托管。
In short, you cannot write a VBA only app that is compiled to an .EXE.
简而言之,您不能编写仅编译为 .EXE 的 VBA 应用程序。
回答by phoog
To create a stand-alone VBA-like program, you would need to use Visual Basic 6 or earlier. The successor of Visual Basic 6, of course, is VB.NET, which is a very different animal.
要创建独立的类似 VBA 的程序,您需要使用 Visual Basic 6 或更早版本。当然,Visual Basic 6 的继承者是 VB.NET,它是一个非常不同的动物。
回答by qwerty
VBA can be licensed, and there are quite a few pproducts outside office that use VBA. MS no longer issues new licenses. There are non-MS VBA implementations, like from Summit software. In any case you need to have your own product that would host VBA.
VBA 是可以授权的,office 外有不少pproduct 使用VBA。MS 不再颁发新许可证。有非 MS VBA 实现,例如来自 Summit 软件。在任何情况下,您都需要拥有自己的产品来承载 VBA。
回答by Michiel van der Blonk
A notable application supporting VBA is AutoDesk AutoCAD. It licenses the VBA runtime and has it's own object model.
一个著名的支持 VBA 的应用程序是 AutoDesk AutoCAD。它许可 VBA 运行时并拥有自己的对象模型。
回答by mischab1
If you don't compile the program ahead of time (in the Visual Basic Editor click Debug -> Compile), then Microsoft Office compiles it at run time. You really should compile frequently though because that is how you find compile errors.
如果您没有提前编译程序(在 Visual Basic 编辑器中单击调试 -> 编译),Microsoft Office 将在运行时编译它。不过,您确实应该经常编译,因为这就是您发现编译错误的方式。
How you run a VBA application depends entirely on how you have set it up to run. In Excel for example you can have the Workbook_Open event start your code when the workbook is opened or create custom menus that users click on to run the code. In Access, you can set a form to display when the database opens or create an autoexec macro that will run when the database opens. etc. etc.
您如何运行 VBA 应用程序完全取决于您如何设置它来运行。例如,在 Excel 中,您可以让 Workbook_Open 事件在工作簿打开时启动您的代码,或者创建用户单击以运行代码的自定义菜单。在 Access 中,您可以设置一个表单以在数据库打开时显示,或者创建一个在数据库打开时运行的 autoexec 宏。等等等等
Like someone else said above, you can't create .exe files of VBA. The code has to run in a Microsoft Office Application.
就像上面其他人所说的那样,您无法创建 VBA 的 .exe 文件。代码必须在 Microsoft Office 应用程序中运行。