使用 VBA 编辑器创建 DLL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18685995/
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 23:13:01  来源:igfitidea点击:

Create a DLL using VBA editor

vbadll

提问by user2759242

I have a position where I essentialy write Visual Basic Macros full time to support a number of divisions in the company. I've done quite well, learned quite a bit, and enjoy my position quite a bit.

我有一个职位,我主要全职编写 Visual Basic 宏来支持公司的多个部门。我做得很好,学到了很多,也很享受我的职位。

We're laying out plans for the future, and while I'm quite good at searching for information I need for these projects (and stack overflow has been a great resource in that), I've come up dry on something I've been trying to do for a bit.

我们正在为未来制定计划,虽然我很擅长为这些项目搜索我需要的信息(堆栈溢出是一个很好的资源),但我已经干了一些我已经一直在努力做一点。

Here is the lay of the land. We distribute macros attached to various other programs via a network drive that everyone in the company has access to. These macros also interface with and automate a proprietary system our company uses. As such, when slight alterations to that system are made, we have to make alterations to a large number of macros in order to stay up to date.

这里是这片土地。我们通过公司中的每个人都可以访问的网络驱动器分发附加到各种其他程序的宏。这些宏还与我们公司使用的专有系统接口并使其自动化。因此,当对该系统进行轻微改动时,我们必须对大量宏进行改动以保持最新状态。

As such, what we would like to try to do is to make a DLL of methods for interacting with our proprietary system. We would like to place that DLL on the network drive, and set the reference for all our macros there. Unfortunately, we do not, at least for the moment, have access to visual studio. All we have access to as far as development environments go is Visual Basic 6.5 as it comes packaged with office. We're working on getting more, but any of you that work at a large company know how that goes.

因此,我们想要尝试做的是制作一个包含与我们专有系统交互的方法的 DLL。我们想将该 DLL 放在网络驱动器上,并在那里设置我们所有宏的引用。不幸的是,我们至少目前无法访问 Visual Studio。就开发环境而言,我们所能访问的只有 Visual Basic 6.5,因为它与 office 打包在一起。我们正在努力获得更多,但在大公司工作的任何人都知道这是怎么回事。

So, I'd like to know if it is possible to create a DLL using the visual basic editor, and if I should expect any issues having dozens of macros potentially accessing it around the same time.

所以,我想知道是否可以使用 Visual Basic 编辑器创建一个 DLL,以及我是否应该预料到有数十个宏可能同时访问它的任何问题。

Thanks in advance for any help or advice you may be able to offer.

预先感谢您提供的任何帮助或建议。

回答by zedfoxus

I assume you are using Office 2007 products. VBA is a scripting language, using which you build your macros. DLLs are compiled assemblies. VBA editor will not allow you to create a DLL.

我假设您使用的是 Office 2007 产品。VBA 是一种脚本语言,您可以使用它来构建宏。DLL 是编译的程序集。VBA 编辑器不允许您创建 DLL。

You could use Visual Basic (not Visual Basic for Applications) to create a DLL and then reference them in your macros.

您可以使用 Visual Basic(而不是 Visual Basic for Applications)来创建一个 DLL,然后在您的宏中引用它们。

You can also use .NET to create an unmanaged export library as mentioned in this postand then reference it in your macros. Although not necessary, I'd recommend using Visual Studio to build a DLL.

您还可以使用 .NET 创建一个非托管导出库,如本文所述,然后在您的宏中引用它。虽然不是必需的,但我建议使用 Visual Studio 来构建 DLL。

I'll state the obvious - why not invest in building a .NET application (or Java or something more enterprisy) than maintaining macros? You may be able to build a flexible configuration so that when your proprietary system changes, the application intelligently changes its parameters also. This may take time but it will reduce maintenance through macros.

我将陈述显而易见的 - 为什么不投资于构建 .NET 应用程序(或 Java 或其他更具企业精神的东西)而不是维护宏?您可以构建灵活的配置,以便当您的专有系统发生变化时,应用程序也会智能地更改其参数。这可能需要时间,但会减少通过宏进行的维护。

It is possible that you don't have a choice. In that case, I'd recommend splitting out your macros in a way that only one or two have to change instead of many. Reduce dependencies appropriately and all the change-prone macros could be brought into a single location. Again, that may not be feasible. In that case, take all the macros and combine them into web services (you could use open source PHP/Python etc. if Visual Studio/.NET isn't available) and then change your office products to call web services. A post about calling web services in excel is here.

您可能别无选择。在这种情况下,我建议您以一种只需更改一两个而不是许多的方式拆分您的宏。适当减少依赖关系,所有易于更改的宏都可以放在一个位置。同样,这可能不可行。在这种情况下,将所有宏合并到 Web 服务中(如果 Visual Studio/.NET 不可用,您可以使用开源 PHP/Python 等),然后更改您的办公产品以调用 Web 服务。关于在 excel 中调用 Web 服务的帖子是here

回答by Joe

Although you can't build a DLL with VBA, you can build an Excel Add-In (.xla) with shared macros and store it on your network drive.

虽然您无法使用 VBA 构建 DLL,但您可以使用共享宏构建 Excel 加载项 (.xla) 并将其存储在您的网络驱动器上。