How to launch VBA code from C#

Posted on 2008-10-10
Medium Priority
Last Modified: 2013-12-17
We are converting some of our reporting code and need to be able to open an Excel template (got that) and then launch a VBA code in that template, and pass a parameter. Later on we will be replacing all the VBA code, but for now I just want to fire it.
Question by:leonstryker

Expert Comment

ID: 22691088
1. Create a dll using the VBA Code
2. Add this DLL as a reference to your C# code
3. Call the function you want
LVL 48

Accepted Solution

Wayne Taylor (webtubbs) earned 2000 total points
ID: 22692250
Hi leonstryker,

To run VBA present in a Workbook, use somthing like this....

    xlApp.Run("'" + xlWB.Name + "'!MacroName", "Param1", "Param2");

...where xlApp is the Excel Application object and xlWB is the Workbook object. The Run method allows up to 30 parameters.


LVL 29

Author Comment

ID: 22692715
Sorry RishadanPort, but the idea is to run the current code which already exist in VBA

Thanks Wayne, I will give it a try on Monday and let you know.


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
From store locators to asset tracking and route optimization, learn how leading companies are using Google Maps APIs throughout the customer journey to increase checkout conversions, boost user engagement, and optimize order fulfillment. Powered …

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question