loading vba code in excel from within C#

G

Guest

Hello,

I generate a new excel file from with code (C#), in this new excel I write
data, ... As a final step I need to call some code (formatting written as a
macro and stored in a module.bas). I want to load this module dynamically
from within my newly created excel file, but I don't find how I can do this.
Can anybody help me with this?

Thanks,
Laura G.
 
J

Jean-Yves

Hi,

Just found in my data that Chip Pearson shows on his site how to do this.
Regards
JY
 
G

Guest

Try

xlApplication.Run "MacroName"

where
xlApplication is your Excel.Application variable
"MacroName" is the name of the subroutine you want to execute.
 
G

Guest

The sytax I gave is for VB. There should be an equivalent for C#. The main
point is to use the Run method of the Excel Application object.
 
G

Guest

I found my answer indeed; for others looking at this:

app = new Excel.Application();
app.Workbooks.Open(filename,Type.Missing, false, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);

Excel.Workbook wb = app.Workbooks[1];

//open a saved module ex: c:\module1.bas
wb.VBProject.VBComponents.Import(vbFileName);

app.Run(macroName, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing);

wb.Save();
app.Quit();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top