Skip to content
This repository has been archived by the owner on Mar 9, 2020. It is now read-only.

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

Open
PrameetS opened this issue Sep 24, 2017 · 3 comments
Open

Comments

@PrameetS
Copy link

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
Copy link

Any updates on this? same problem thanks.

@PrameetS
Copy link
Author

No Updates, let me know if u get anything

@hurzeler
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants