MNB PHPExcelDeveloper Guide
v2.0.5

Automation examples

Automation Excel examples, explained clearly.

Open a focused example to see the implementation path, copy-ready PHP, expected workbook behavior, and the production boundary that matters.

3examples in this category
21focused categories
5implementation paths
01
Record Macro

Use Excel to record the macro in a trusted .xlsm template; PHP supplies fresh data without running the macro.

MediumTrusted template
preserveAdvancedObjectsFrom()
record-macro.php
<?php

use Mnb\PHPExcel\MnbExcel;

MnbExcel::fromArray($rows)
    ->withHeader()
    ->preserveAdvancedObjectsFrom('templates/monthly-report.xlsm')
    ->save('monthly-report-output.xlsm');
02
Edit VBA Macro

Edit VBA in Excel or a dedicated VBA tool, then preserve the approved macro project while refreshing workbook data.

AdvancedTrusted template
MnbExcel::validateUpload()preserveAdvancedObjectsFrom()
edit-vba-macro.php
<?php

use Mnb\PHPExcel\MnbExcel;

$inspection = MnbExcel::validateUpload('templates/approved-automation.xlsm', [
    'allowed_extensions' => ['xlsm'],
    'reject_macros' => false,
]);
if (!($inspection['features']['macros'] ?? false)) {
    throw new RuntimeException('Expected an approved macro project.');
}

MnbExcel::fromArray($rows)
    ->withHeader()
    ->preserveAdvancedObjectsFrom('templates/approved-automation.xlsm')
    ->save('automation-output.xlsm');
03
Create User Form

Create the VBA UserForm in a trusted macro-enabled template and preserve it while replacing worksheet data.

AdvancedTrusted template
preserveAdvancedObjectsFrom()
create-user-form.php
<?php

use Mnb\PHPExcel\MnbExcel;

MnbExcel::fromWorkbookArray([
    'Data' => $rows,
    'Lists' => $dropdownValues,
])
    ->withHeader()
    ->preserveAdvancedObjectsFrom('templates/intake-userform.xlsm')
    ->save('intake-userform-output.xlsm');