Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sheet-based macros are removed on Workbook save #416

Closed
nicojx opened this issue Nov 14, 2022 · 3 comments
Closed

Sheet-based macros are removed on Workbook save #416

nicojx opened this issue Nov 14, 2022 · 3 comments

Comments

@nicojx
Copy link

nicojx commented Nov 14, 2022

Describe the bug
Loading a macro-enabled workbook (via wb_load()) with sheet-based VBA macros (such as Private Sub Worksheet_SelectionChange(ByVal Target As Range)) and saving it again using wb_save() results in the removal of those macros from the sheets.

What seems to happen is that openxlsx2 creates copies of the original sheets without macros (the visible sheet name is the same but the Excel-internal sheet name is different), while the original sheets remain with macro but without the data / invisible. This can then be seen in the VBA view sheet list (see screenshot). Originally only one sheet (Sheet1 was both internal and external name) existed.

To Reproduce

  1. Create a Macro-enabled Excel file
  2. Add a sheet-based macro (such as Private Sub Worksheet_SelectionChange(ByVal Target As Range)). This doesn't have to 'do' anything. Save.
  3. View the sheets in the Excel macro view (Alt+F11)
  4. Load the workbook via wb_load()
  5. Save the workbook object via wb_save()
  6. Re-open the workbook in Excel, and examine the sheets in the macro view (Alt+F11)

Expected behavior
The same sheets exist after save as before save, with macros attached to sheets.
The issue does not appear on non-macro-enabled (xlsx) workbooks.
The openxlsx had the same behaviour.

Screenshots
Screenshot 2022-11-03 230835

Example files
I cannot upload macro-enabled Excel files but the problem should be easily reproducable.

@JanMarvin
Copy link
Owner

Could you please mail me an example? The mail address is in my GitHub profile. Thanks!

@JanMarvin
Copy link
Owner

Hi @nicojx , thanks for providing the example file (and not highjacking me with strange Macros 😅). A potential fix in #417 could you please test this?

remotes::install_github("JanMarvin/openxlsx2", ref = "gh_issue_416")

@nicojx
Copy link
Author

nicojx commented Nov 14, 2022

Great, that fixes it indeed! Thanks so much!

@nicojx nicojx closed this as completed Nov 14, 2022
JanMarvin added a commit that referenced this issue Nov 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants