use strict; use warnings; use Win32::OLE; my $xl = Win32::OLE->new('Excel.Application'); $xl->{Visible} = 1; my $wb = $xl->Workbooks->Add; if ($wb->Sheets->{Count} > 1) { for (2..$wb->Sheets->{Count}) { $wb->Sheets(2)->Delete; } } $xl->VBE->ActiveVBProject->VBComponents->Add(1); my $cm = $xl->VBE->ActiveVBProject->VBComponents(3)->CodeModule; my $line = int($cm->CountOfLines); $cm->InsertLines(++$line,"Function Example(n as double) as double"); $cm->InsertLines(++$line,"Example = n^2"); $cm->InsertLines(++$line,"End Function"); my $sht = $wb->Sheets(1); $sht->Range("A1")->{Formula} = "=Example(2)"; $sht->Range("A2")->{Formula} = "=2*2"; my $a1 = $sht->Range("A1")->{Value}; my $a2 = $sht->Range("A2")->{Value}; if ($a1 != 4 or $a2 != 4) {print "We've had a problem"} else {print "Life is good"};