tasairis wrote:As long as you're talking about running the macro on a XLS file on the server,
COM can probably do it. Three downsides:
1. It doesn't work on Unix systems.
2. It requires that Office (at least Excel) is installed on the server.
3. It can be hard to research the right code.
Tip: Open up Excel and record a macro. Once recording, do whatever you want automated (in your case, run a macro). Then stop recording. Open up the script editor (whatever it's called) and find the code for the macro. That can be very nearly copied and pasted into your PHP (requires changing the syntax and such).
Hi,
Thank you.
I had a look and it looks promising. I found some code that I tried to run but I get this error all the time, from all of the codes:
Fatal error: Uncaught exception 'com_exception' with message 'Error [0x8002000e]
Ogiltigt antal parametrar. ' in C:\wamp\www\Kiwi\batch\phpCreateExcel_1.php:31 Stack trace: #0 C:\wamp\www\Kiwi\batch\phpCreateExcel_1.php(31): variant->Save('test1.xls') #1 {main} thrown in C:\wamp\www\Kiwi\batch\phpCreateExcel_1.php on line 31
'Ogiltigt antal parametrar', text in red is swedish and basically means 'wrong number of parameters.
When I look at VBA code to save it seems like I should use 'SaveAs' but then I get:
Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft Office Excel<br/><b>Description:</b>
Kunde inte hitta SaveAs egenskapen för Workbook klassen' in C:\wamp\www\Kiwi\batch\phpCreateExcel_1.php:31 Stack trace: #0 C:\wamp\www\Kiwi\batch\phpCreateExcel_1.php(31): variant->SaveAs('test1.xls') #1 {main} thrown in C:\wamp\www\Kiwi\batch\phpCreateExcel_1.php on line 31
'Kunde inte hitta SaveAs egenskapen för Workbook klassen', text in red means something like 'Could not find SaveAs property for Workbook class'
This is the code:
<?php
// set up excel
$excel = new COM("excel.application") or die("Unable to instantiate excel");
// run Excel silently, since we don’t want dialog boxes popping up in the background
$excel->DisplayAlerts = false;
// open up Excel file and select the first sheet, which contains the inputs
$excel->Workbooks->Add();
$book = $excel->ActiveWorkbook;
$sheets = $book->Sheets;
$sheet = $book->Worksheets(1);
$cell = $sheet->Cells(1, 1);
$cell->Activate;
$cell->value = "myValue";
/*
$input = array("kalle", "Anka", "And", "Duck");
// input stuff into excel spreadsheet
for ($i = 0; $i < count($input); $i++) {
$cell = $sheet->Cells(1, $i + 1);
$cell->Activate;
$cell->value = $input[$i];
}
*/
// run macro
//$excel->Run("runModel");
// save spreadsheet
$book->SaveAs("test1.xls");
// quit Excel and clean up
$book->Close(false);
unset($sheets);
$excel->Workbooks->Close();
unset($book);
$excel->Quit;
unset($excel);
?>