run vba code from .net applications

2019-05-27 04:28发布

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! :)

标签: .net vba dll com
3条回答
淡お忘
3楼-- · 2019-05-27 05:03

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.

查看更多
仙女界的扛把子
4楼-- · 2019-05-27 05:15

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

查看更多
登录 后发表回答