JanKallman / EPPlus

Create advanced Excel spreadsheets using .NET
3.76k stars 1.18k forks source link

VBA code issue while copying a worksheet from template worksheet #7

Open PrameetS opened 7 years ago

PrameetS commented 7 years ago

Hello EPPLUS team,

I am having an issue while copying a ExcelWorkSheet and the corresponding CodeModule of the copied sheet in 4.1 EPPLUS version.

I have an excel file with macro's (.xlsm) and I use this excel file as a template to create another excel file.

I open the excel template (.xlsm) file and fetch some data from a database which needs to be written in new file.

Some time I need to copy a worksheet in template file, this is where the 'WorkSheets.Add()' comes in.

var newSheet = workbook.Worksheets.Add("someName", templateSheet);

After copying the template sheet and writing data on it, I am experiencing problems with VBA code. When I try to open the new excel file, it gives me below error.

The Visual Basic for applications (VBA) macros in this workbook are corrupted and have been deleted. The macro corruption most likely exists in the current file.

Below is the code from 'WorkSheets.Add("someName", templateSheet)' method: `//Copy the VBA code

if !MONO

            if (_pck.Workbook.VbaProject != null)
            {
                var name = _pck.Workbook.VbaProject.GetModuleNameFromWorksheet(added);
                _pck.Workbook.VbaProject.Modules.Add(new ExcelVBAModule(added.CodeNameChange) { Name = name, Code = Copy.CodeModule.Code, Attributes = _pck.Workbook.VbaProject.GetDocumentAttributes(Name, "0{00020820-0000-0000-C000-000000000046}"), Type = eModuleType.Document, HelpContext = 0 });
                Copy.CodeModuleName = name;
            }

endif`

I am not able to understand why in the last line epplus is changing the code module name of template sheet instead of newly added sheet?

Copy.CodeModuleName = name;

Is this a bug which causes the VBA code to be corrupted?

Also, when trying to change the code module name through below code

templateSheet.CodeModule.Name = "oldName";

causes templateSheet.CodeModule set to null.

Please look into it and resolve it.

Thanks, Prameet

RidhwanDev commented 5 years ago

Any updates on this? same problem thanks.

PrameetS commented 5 years ago

No Updates, let me know if u get anything

hurzeler commented 5 years ago

Is this fixed? I still have the same issue on EPPlus Version 4.5.3.2. It drops an A on ttribute VB_Name = This Workbook". I.e. the code is corrupt after the copy.