VBA Macros not active in xlsm document

Issue #716 duplicate
Wolfgang Mayer
created an issue

It seems that VBA macros are not active in .xlsm files written by openpyxl.

The attached Workbook1.xlsm file contains a VBA subroutine that reacts to double-click events. (The specific effect of the macro is not important in this case.)

This file was created using Excel 15.27 on macOS Sierra. When opening the file in Excel (on the same host and Excel version), the usual "This workbook contains macros. ..." prompt appears and, if macros are enabled, the file works as expected.

However, it I read and subsequently save the xlsm file using openpyxl, no macro warning prompt appears and the VBA subroutine does not appear to be active when the file is opened in Excel (on Mac OS). (I tried the 2.4 openpyxl release as well as a checkout from bitbucket - the result was the same.)

I have attached the input file (Workbook1.xlsm), the file written by openpyxl (copy.xlsm), and the python program creating said file (copy_sheet.py).

Comparing the contents of the two files, it looks like the culprit is in file "Content_Types].xml", where the "bin" extension is listed with content type "application/vnd.ms-office.activeX" in copy.xlsm, whereas it has content type "application/vnd.ms-office.vbaProject" in Workbook1.xlsm.

Indeed, if the initialisation of the mime types in openpyxl's source code in file openpyxl/packaging/manifest.py is changed slightly, the macros in the file output by openpyxl are detected and work as expected in Excel 15.27. Specifically, the line mimetypes.add_type("application/vnd.ms-office.activeX", ".bin") was changed to mimetypes.add_type("application/vnd.ms-office.vbaProject", ".bin")

Is this a bug, or am I using openpyxl incorrectly?

Thanks for the great work on openpyxl.

Comments (5)

  1. CharlieC

    The default branch for us is always that most recent stable release. We know a lot of projects use it as the the bleeding edge but we don't think that is good practice.

  2. Log in to comment