ArmandoG has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks I am working on a little script to automatize a report, so the thing is that I come to a complete halt when they told me the need to print it too.

But I have no Idea on how to do this?

this is the code I have right now

use Win32::OLE; use Win32::OLE::Variant; use Win32::OLE::Const 'Microsoft Excel'; my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); my $Book = $Excel->Workbooks->Open("C:\\Projectword\\datossema.xls"); my $Sheet = $Book->Worksheets(1); my $excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit +'); my $Libro = $excel->Workbooks->Open("c:\\Projectword\\datafill.xls +"); my $Hoja = $Libro->Worksheets(1); #my $UltimaRegla = $Hoja->UsedRange->Find({What=>"*", # SearchDirection=>xlPrevious, # SearchOrder=>xlByRows})->{Row}; # #my $Renglon = $UltimaRegla + 25; $Sheet->Range("A1")->Copy(); $Sheet->Paste ({Destination => $Hoja->Range("H9")}); $Hoja -> Range("H9") -> Font -> {Size}=7; $Sheet->Range("B1")->Copy(); $Sheet->Paste ({Destination => $Hoja->Range("E10")}); $Hoja -> Range("E10") -> Font -> {Size}=7; $Sheet->Range("C1")->Copy(); $Sheet->Paste ({Destination => $Hoja->Range("F10")}); $Hoja -> Range("F10") -> Font -> {Size}=7; $Sheet->Range("D1")->Copy(); $Sheet->Paste ({Destination => $Hoja->Range("G10")}); $Hoja -> Range("G10") -> Font -> {Size}=7; $Sheet->Range("E1")->Copy();
I really apreciate if someone can put an example thanks

Replies are listed 'Best First'.
Re: Printing from Excel
by olus (Curate) on Feb 15, 2008 at 15:59 UTC
Re: Printing from Excel
by whereiskurt (Friar) on Feb 15, 2008 at 15:55 UTC
    ArmandoG,

    Are you asking how to print through Excel using OLE automation?

    Although, strictly speaking this isn't a "Perl" solution, may I suggest the follwing:

    Inside of Excel, start recording a Macro (Tools->Macro->Record a Macro...), and do the things you want to do -- like Printing a range. Then, go and review the VBA code the macro made for you (ALT-F11). That VBA code will reveal the methods you can call on the various objects (Sheet, Application, etc.) to achieve your goal.

    Here's the VBA code from a macro that prints a selected range:

    Range("A4:F20").Select ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True

    And here's roughly (untested) what you might need to do to make it work with your Perl OLE objects:

    $Sheet->Range("A4:F20")->Select(); $Excel->SelectedSheets->PrintOut({Copies=>1, Collate=>True});

    Anyhow... something like that.

    Good luck!

    Kurt

Re: Printing from Excel
by marto (Cardinal) on Feb 15, 2008 at 15:54 UTC
    Hi ArmandoG,

    Try something like $Book->ActiveSheet->PrintOut();.

    Hope this helps

    Martin