Click here to Skip to main content
15,887,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have one macro called "countMacro" and one function called "countCell()"
I have tested in excel sheet it works fine.
But i want to add this function/macro using C#, at run time i have to add macro to dynamic excel and macro will be run and result will be shown on cell.
I am using Epplus library.
do reply.
Thank you.
Posted
Updated 2-Aug-20 4:10am
v2

hi,
http://www.nullskull.com/articles/create_macro_at_runtime_in_dotnet.asp[^]
think this will help you. But in this article their are using Interop.Microsoft.Office.Core.dll to create excel so by using this you can add macro to excel.
 
Share this answer
 
Comments
Thank you for response .
I have used Epplus library to load my data into Excel sheet , now i want to add macro into excel sheet using Epplus library.is there any way do this ?
Thank you.
navi_s_Patil 11-Apr-14 3:23am    
hi,
EPPLUS will not Support for VBA Macros,
http://codingsense.wordpress.com/2009/05/11/create-execute-and-delete-macro-in-excel-using-c/ check out dis link and yo can also load the data using Microsoft.Office.Interop.Excel pity much same as like epplus.
Brady Kelly 22-Nov-19 7:39am    
EPPlus does support *creating* macros. See solution 3.
Solution 3 is almost OK (for EPPlus 4.5.3.3).
After opening Visual Basic you can see that Module1 is not put inside "Modules" but inside "Microsoft Excel Objects" along with sheets. Because of this macros are not seen as executable from within Excel (like after pressing Alt+F8).

Here you can see modification that puts Module1 where it should be:

var xlPack = new ExcelPackage(excelFile);
xlPack.Workbook.CreateVBAProject();
var module = xlPack.Workbook.VbaProject.Modules.AddModule("Module1");
module.Code = "Sub countMacro()\r\n ... Place Code Here ... \r\nEnd Sub\r\n";


BTW. I recommend to put macro code in .NET resources and load it:

var embeddedProvider = new EmbeddedFileProvider(Assembly.GetExecutingAssembly());
using (var reader = embeddedProvider.GetFileInfo("Resources/ExcelMacros.vb").CreateReadStream())
using (var textReader = new StreamReader(reader))
{
    xlPack.Workbook.CreateVBAProject();
    var module = xlPack.Workbook.VbaProject.Modules.AddModule("Module1");
    module.Code = textReader.ReadToEnd();
}
 
Share this answer
 
v2
ExcelPackage xlPack = new ExcelPackage(excelFile);	// Open or Create the File
// Add VBA Code
xlPack.Workbook.CreateVBAProject();			// Can not append VBA Projects.Multiple "Code" can be added. See "https://github.com/pruiz/EPPlus/blob/master/SampleApp/Sample15.cs".
xlPack.Workbook.CodeModule.Name = "Module1";
xlPack.Workbook.CodeModule.Code = "Sub countMacro()\r\n" +
" ... Place Code Here ... \r\n" +
"End Sub\r\n";
 
Share this answer
 
Comments
Patrice T 2-Jun-17 22:15pm    
Only 3 years too late
Andrzej Martyna 2-Aug-20 3:51am    
But still useful. I've found it today and it's exactly what I was looking for. The sense of time in Internet is not so obvious as in real life ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900