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; #Comment 1 } } $xl->VBE->ActiveVBProject->VBComponents->Add(1); #Comment 2 my $cm = $xl->VBE->ActiveVBProject->VBComponents(3)->CodeModule; #Comment 3 my $line = int($cm->CountOfLines); #Comment 4 $cm->InsertLines(++$line,"Function Example(n as double) as double"); #Comment 5 $cm->InsertLines(++$line,"Example = n^2"); $cm->InsertLines(++$line,"End Function"); my $filename = "z:\\data\\perl\\Example.bas"; #Comment 6 $xl->VBE->ActiveVBProject->VBComponents(3)->Export($filename); #Comment 7 $xl->{DisplayAlerts} = 0; #Comment 8 $wb->Close; $wb = $xl->Workbooks->Add; $xl->VBE->ActiveVBProject->VBComponents->Import($filename); #Comment 9