ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   loading vba code in excel from within C# (https://www.excelbanter.com/excel-programming/385560-loading-vba-code-excel-within-c.html)

Laura G.[_2_]

loading vba code in excel from within C#
 
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.

Jean-Yves[_2_]

loading vba code in excel from within C#
 
Hi,

Just found in my data that Chip Pearson shows on his site how to do this.
Regards
JY
"Laura G." wrote in message
...
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.




Vergel Adriano

loading vba code in excel from within C#
 
Try

xlApplication.Run "MacroName"

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

"Laura G." wrote:

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.


Vergel Adriano

loading vba code in excel from within C#
 
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.


"Vergel Adriano" wrote:

Try

xlApplication.Run "MacroName"

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

"Laura G." wrote:

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.


Laura G.[_2_]

loading vba code in excel from within C#
 
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();

"Jean-Yves" wrote:

Hi,

Just found in my data that Chip Pearson shows on his site how to do this.
Regards
JY
"Laura G." wrote in message
...
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.






All times are GMT +1. The time now is 05:03 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com