i have a vba project i want to make the same in c# wpf so is there any way that i can use the same project and just make UI using wpf ? is there any way like creating dll of this vba project and adding it in new wpf project and call the API's of existing project.
1 Answer
Not exactly. VBA is a crippled version of VB6, so you don't have full type library publishing capabilities. That said, it is possible (though rather painful) to have VBA implement typelib-published COM interfaces, then expose those to managed code and call them via COM interop from any C# code you like (WPF included).
Basically, you need to:
- Define an IDispatch or dual interface
- Publish it in a type library
- Reference the typelib into your VBA project (and your C# project if it didn't originate there)
- Write a VBA class module that implements the new interface
- Set the class' instancing property to PublicNotCreatable
- Write a shim function in a VBA (non-class) module that you can call from C# via Excel's Application.Run method- this function should create an instance of the VBA class and return it. The calling C# code casts the returned value to the managed version of the IDispatch or dual interface you declared earlier.
Voila- now you can call your VBA code directly from C#. It was years ago, but I've done this with a gigantic legacy VBA codebase with great success. It was a very painful process, though, and fraught with peril. It's easy to leak interfaces, there are a number of issues around COM interface and typelib versioning, and threading issues abound.
All that said, if you don't know how to do most of these things, or can't quickly find the resources you need, you probably ought to consider porting the VBA to C#.
Good luck!