jmcnamara / libxlsxwriter

A C library for creating Excel XLSX files.
https://libxlsxwriter.github.io
Other
1.48k stars 330 forks source link

New feature: Add signature file for VBA projects #411

Closed HolgiHo closed 11 months ago

HolgiHo commented 11 months ago

VBA projects can be digitally signed in Excel.

This is an important security feature for many commercial customers: Administrators or users can choose whether to block execution of unsigned VBA macros in their environment, for example, by editing a setting in the Excel Security Center dialog.

For details see the official documentation: https://support.microsoft.com/en-us/office/change-macro-security-settings-in-excel-a97c09d2-c082-46b8-b19f-e8621e8fe373

Technically, the VBA signature file is named vbaProjectSignature.bin and is located in the same folder as vbaProject.bin. Some small additional changes are required in the xlsm file data file to make Excel accept the digital signature.

Digital signatures are added in VBA code editor window, menu Tools -> Digital Signatures. For details, again see the official documentation: https://support.microsoft.com/en-us/office/digitally-sign-your-vba-macro-project-956e9cc8-bbf6-4365-8bfa-98505ecd1c01

This feature was already requested in 2015 in the Python version of the library, see https://github.com/jmcnamara/XlsxWriter/issues/283. However, the PR never get merged as it missed the required contribution standards.

I have extended libxlsxwriter to support adding a VBA project digital signature file, including an example, a functional test, and documentation.

Here is an example for the C library. Note the new public function workbook_add_vba_project_signature. This is the only API extension I made.

lxw_workbook  *workbook  = workbook_new("macro_signed.xlsm");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);

/* Add a macro file extracted from an Excel workbook. */
workbook_add_vba_project(workbook, "vbaProject.bin");

/* Add a VBA project signature file extracted from an Excel workbook. */
workbook_add_vba_project_signature(workbook, "vbaProjectSignature.bin");

I will create the corresponding PR soon and put a reference here.

I have also added the feature to the Python version of the library, https://github.com/jmcnamara/XlsxWriter. I will open a similar issue there as well.

jmcnamara commented 11 months ago

Merged on main.