从.net应用程序运行vba代码

发布于 2024-10-01 07:35:17 字数 631 浏览 1 评论 0原文

我有大量用 VBA 编写的代码,需要迁移到 .Net 应用程序。

重写所有内容是不切实际的,因为太多了,所以我想知道是否有一种方法可以从 .NET 调用这些 vba 函数,

谢谢。

更新:

我一直在调查这些内容,结果发现它们并不合适。该应用程序不是 Microsoft Office,并且不公开任何类型的“运行”宏功能。

对我们的旧第三方应用程序和示例记事本应用程序生成的文件进行比较后,文件格式(在记事本中查看)看起来非常相似(前几个字符相同,总体布局相同),所以我想知道是否也许有一种方法可以使用此项目加载/保存功能来访问宏。

我已经做了一些基本测试,尝试将现有的 vba 加载到记事本 vba 中是行不通的。我猜我需要更新应用程序的界面,但是,不幸的是,似乎不可能再获得 VBA 许可协议,因为微软已经决定每个人都应该使用 VSTA - 所以我不确定最好的方法继续进行此操作。

假设没有办法将 VBA 集成到我们的新应用程序中,我唯一剩下的想法是在旧应用程序中创建一个新的 VBA 函数,该函数在 application_start 或 period_execute 入口点上调用,并让它们能够设置所需的全局变量并调用其他函数,但是我仍然希望有更好的东西。

欢迎任何想法! :)

I have a very large amount of code written in VBA that I need to migrate to a .Net application.

It isn't practical to rewrite all of it as there is just too much, so I was wondering if there is a way to call these vba functions from .NET

Thanks.

Update:

I've been investigating these and it turns out they aren't appropriate. The application isn't Microsoft Office and doesn't expose any sort of "Run" macro functionality.

Having done a comparison of the files produced by our legacy third party application and by the sample notepad application, the file formats (viewed in notepad) look very similar (same first few characters, general layout is the same), so I was wondering if there was perhaps a way to use this project load/save functionality to access the macros.

I've done some basic tests, attempting to just load our existing vba into the notepad vba doesn't work. I'm guessing I need to update the application's interface, however, unfortunately it seems it isn't possible to get a VBA license agreement anymore as Microsoft have decided everyone should be using VSTA instead - so i'm not sure of the best way to proceed with this.

Assuming there isn't a way to integrate VBA into our new application my only remaining idea is to create a new VBA function in the legacy application which is called on the application_start or periodic_execute entry points, and have these be able to set up the required global variables and call other functions, however I'm still hoping for something nicer.

Any ideas welcome! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长亭外,古道边 2024-10-08 07:35:17

我在这里假设您的 VBA 代码包含在 Microsoft Office 应用程序之一中。

所有 Microsoft Office 应用程序都公开 Application.Run() 方法,该方法接受
宏或子过程的名称。宏或子过程必须声明为 Public。

您可以使用 .Net 代码创建相关 Office 应用程序(Word、Excel 等)的实例,然后通过调用 Run 方法来调用宏或 Sub 过程,将宏/过程名称作为字符串参数传递,例如

objWordApp.Run("MyVBAMacro")

Run方法还接受许多参数,这些参数可以包含您需要提供给宏或过程的任何参数的值。

MSDN 文章包含一些示例。

I am assuming here that your VBA code is contained in one of the Microsoft Office applications.

All the Microsoft Office applications expose the Application.Run() method which accepts
the name of a macro or sub procedure. The macro or sub procedure must be declared Public.

You can use .Net code to create an instance of the relevant Office application (Word, Excel, etc.) and then invoke the macro or Sub procedure by calling the Run method, passing the macro/procedure name as a string argument e.g.

objWordApp.Run("MyVBAMacro")

The Run method also accepts a number of arguments which can contain the values of any parameters you need to supply to the macro or procedure.

This MSDN article contains some examples.

懵少女 2024-10-08 07:35:17

也许你可以尝试将代码转换为VSTO。有关于此的一些帖子可以在这里找到:

http://blogs.officezealot .com/hansen/archive/2007/01/10/20048.aspx

Maybe you can try to convert the code into VSTO. Some posts about this are found here:

http://blogs.officezealot.com/hansen/archive/2007/01/10/20048.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文