I know that it is not possible to create new macro using NPOI, but according to the document, already existed macro should be preserved on copy:
Macros can not be created. The are currently no plans to support macros. However, reading and re-writing files containing macros will safely preserve the macros. Recent versions of Apache POI support extracting the macro data via VBAMacroExtractor and VBAMacroReader
However, when I tried to copy a xlsm file using NPOI, I ran into the following problem:
- In my xlsm file, I have a button with an onclick function named "CopyDetails_Click".
- When I copy that file, all other macro in the new file still works as expected, but in Macro Registration menu of my button, the name of the onclick function is changed to a seemly random value, for example "Button1_Click".
- Because of that, the button no longer works, I have to manually changed the onclick function name back to "CopyDetails_Click". This seems to be the only problem I have, all other macro functions works as expected.
Is this a bug of Apache POI or a bug of NPOI, and how do I keep the correct onclick function name when I copy a xlsm file?
I'm using .Net Framework 4.6.2, NPOI 2.3.0 and trying to copy an Excel 2013 file.
Update 1: I found out that only buttons placed directly on the sheet are effected by this error. If I place my buttons inside a form then its macro name will be preserved after copy, and they will work as expected.