Persisting macros to new xlsm results in Workbook_Open method not working #615
Labels
No Label
DBF
Dates
Defined Names
Features
Formula
HTML
Images
Infrastructure
Integration
International
ODS
Operations
Performance
PivotTables
Pro
Protection
Read Bug
SSF
SYLK
Style
Write Bug
good first issue
No Milestone
No Assignees
1 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: sheetjs/sheetjs#615
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
After the changes made for issue #606 i am now able to put the macros onto the newly written workbook, but any code in the ThisWorkbook excel object (which holds all methods relating to Workbook-wide code like Workbook_Open to do operations when you open the workbook, or Workbook_SheetChange to detect changes to any sheet in the workbook) no longer works.
From what I'm seeing, the newly-written xlsm file has a ThisWorkbook and a ThisWorkbook1, and it doesn't allow for the globals to work from ThisWorkbook, and I don't want to force end users to go into the code and move things over. I also can't delete ThisWorkbook1 from Excel's Visual Basic editor.
To show this, try doing the following:
You'll see that there's a new sheet (as desired) but that the button that was supposed to be created on workbook startup doesn't show up and that there's a new object called ThisWorkbook1.
Also, by my local testing, it doesn't make a ThisWorkbook1 object if there is no code in the ThisWorkbook object.
Thanks for looking into this! The fix is very easy this time :)
The worksheets and workbook have a properties tag that maps the VBA code names to the sheet names.
If you can tinker with the source, there are two changes:
write_wb_xml
, add acodeName
:If you make just this change, you will see the ThisWorkbook1 stuff is resolved.
write_ws_xml
, add asheetPr
tag with acodeName
:GitHub doesn't allow uploading XLSM files :( so you will have to download and rename to XLSM to verify:
Using XLSX 0.9.7 from NPM: broken.xlsx
Using the patches from the previous comment: fixed.xlsx
Please verify that the fixed file works. We will push the change in the next release, hopefully tomorrow.
It works! But it seems like Excel doesn't like it. See attached for the dialog boxes that pop up after making the changes you suggested with the code i used at the start of this post.
Hmm what version of Excel are you using? I just checked the attached "fixed.xlsm" file against Excel 2016 and Excel 2007, both accept the macro and show the testing button:
@SheetJSDev I'm using 2013. the button still shows up appropriately, but those dialog boxes show up on first time opening the newly made workbook
@johnothetree I still can't reproduce in Excel 2013 :/
fixed.xlsm
I see a message in the top bar:
I click enable editing and see the macro security warning:
After clicking enable content I see the macro worked:
For completeness, I included a complete demo in a gist: https://gist.github.com/SheetJSDev/decd392746c23195696efab1c80d66a2
Just download it and follow the README instructions. On my computer the generated file works in Excel 2013 without the warnings you showed.
huh. yeah the gist works fine. maybe i put something in the wrong spot? whatever, looks to work fine! thanks for the help! i'll work with the official release when it's out!
@SheetJSDev , I am trying your solution, in the second change
what line in the write_ws_xml function should I add the tag?
@MiriamHershowitz no need. https://docs.sheetjs.com/#vba-and-macros Workbook code name
wb.Workbook.WBProps.CodeName
, Worksheet code namewb.Workbook.Sheets[idx].CodeName
Do I have to set the code name?
Yes you do
On Wed, Nov 3, 2021 at 04:00 MiriamHershowitz @.***>
wrote:
I tried to set the CodeName but still can't see the macro buttons, I saw that the Workbook and Worksheet code name are automatically set, do I have to change it from the default value? and if so, what value?
Please share a sample file (you may need to rename to .xlsx to attach to the GH issue)
I am trying to read the file
MacroBook.xlsm
and write it toMacroBook1.xlsm
but after wrting I cant see the button MacroBook.xlsm has
MacroBook.xlsx
Thank U for trying to help...