Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shing_shing's avatar

PHPSpreadsheet removes existing macros

I am writing on an existing template with macros. After saving it, the macros are all removed.

Do you have any hacks in mind?

#PHPSpreassheet

0 likes
3 replies
MarkLL's avatar
MarkLL
Best Answer
Level 7

This looks to be a known problem. See https://github.com/PHPOffice/PhpSpreadsheet/issues/1201 and also https://stackoverflow.com/questions/56855022/how-to-write-macro-in-phpsreadsheet-laravel.

The problem is that a true vbaProject.bin file is actually a binary OLE COM container.

If you create a file from scratch and simple pass a text representation of your vba code, it simply writes the vbaProject.bin file as a text file. This is why Excel reports there was a problem in the file when you open it.

Here is what you could try. Assuming you are reading an existing file (via upload) and then downloading to the user the new file.

Open the file via ZipArchive (a .xlxs/m is actually a zip file) and extract 'xl/vbaProject.bin' and store somewhere (temp file or variable).

Then just before you save the file (for download or whatever) call the $spreadsheet->setMacrosCode() method and supply the binary vbaProject.bin you stored previously.

edit: If you have the time, you can read more about the vbaproject.bin file structure at https://www.decalage.info/vba_tools . You can download the structure documentation at https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-ovba/575462ba-bf67-4190-9fac-c275523c75fc

1 like
shing_shing's avatar

Thank you, Sir @markll , I will look into that. PHPOffice is a big scope of a topic to study. But it's interesting! Thank you for your response! God bless you!

MarkLL's avatar

You're welcome @shing_shing, as you said, it is quite interesting :) Unfortunately, I don't have the time or need at this point to dig any further so good luck with your project. Let us know how you go...

Please or to participate in this conversation.