loading vba code in excel from within C#

  • Thread starter Thread starter Guest
  • Start date Start date
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.
 
Hi,

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

xlApplication.Run "MacroName"

where
xlApplication is your Excel.Application variable
"MacroName" is the name of the subroutine you want to execute.
 
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.
 
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();
 
Back
Top