#!/usr/bin/perl -w use strict; use Win32::OLE; use Tk; use Tk::DialogBox; my $mw = MainWindow->new( -width => 350, -title => 'Gerenciador de Planilhas', -height => 50 ); my $Frame0 = $mw->Frame(); $Frame0->place( -x => 0, -y => 8, -height => 54, -width => 352); #the following button activates the sub that generates the document my $Button1 = $mw->Button( -text => "Criar Planilha", -relief => "raised", -command => \&geraNova ); $Button1->place( -x => 9, -y => 14, -height => 25, -width => 74); my $Button2 = $mw->Button( -text => "Sobre...", -relief => "raised", -command => \&onAbout ); $Button2->place( -x => 203, -y => 14, -height => 25, -width => 64); my $Button3 = $mw->Button( -text => "Sair", -relief => "raised", -command => sub{exit 0} ); $Button3->place( -x => 270, -y => 14, -height => 25, -width => 64); $mw->geometry('350x50'); MainLoop; # Below is the sub that creates the doc sub geraNova { my $ctrl = './control.dat'; open(CTR, "+>>$ctrl") || die "Não foi posspivel abrir o arquivo. Erro: $!"; flock(CTR, 2); my $id = ; flock(CTR, 8); close(CTR); $id = $id + 1; unlink $ctrl; open(CTR, "+>>$ctrl"); flock(CTR, 2); print CTR $id; flock(CTR, 8); close(CTR); $id = addZeros($id); #I've renamed the file after it was created and after this app was closed my $file = 'C:\foo\path1\test'.$id.'.xls'; my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application'); # I've tested with the alerts enabled and there was no error $Excel->{DisplayAlerts} = 0; $Excel->{Visible} = 1; my $Book = $Excel->Workbooks->Add(); $Book->SaveAs($file); my $ActBook = $Excel->Workbooks->Open("$file") || die "Não foi possível abrir a planilha. Erro: $!"; undef $Excel; undef $Book; undef $ActBook; } sub addZeros { my $num = $_[0]; while(length($num) < 6) { $num = "0".$num; } return $num; } sub onAbout { my $about = $mw->DialogBox( -title=>"Sobre...", -buttons=>["OK"] ); $about->add('Label', -anchor => 'w', -justify => 'center', -text => qq( Excel Controller 1.0 by Er Galvão Abbott -Dúvidas, Bugs ou sugestões: galvao\@galvao.eti.br - Visite meu site em: http://www.galvao.eti.br/ ) )->pack; $about->Show(); }