john.tm has asked for the wisdom of the Perl Monks concerning the following question:

I have a code that will print of.txt documents but when i send spreadsheets or word documents to be printed, they print of 100's of pages in binary.Is there any way to print of these sorts of files?
use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; use autodie; use File::Copy; my $errorprn1 = "\n! cannot find printer UK BAS 1ST FLOOR WEST PRN 1 o +n srv00086\n It may be out use, try using UK BAS 1ST FLOOR WEST PRN 2\n"; copy 'C://Temp//test.xls' , '//SRV00086//UK BAS 1ST FLOOR WEST PRN 1' +or die "$errorprn1" ; #my $Word = Win32::OLE->new('Word.Application', 'Quit'); #$Word->{'Visible'} = 1; # if you want to see what's going on #$Word->Documents->Open("C:\\temp\\prn1.doc") #|| die("Unable to open document ", Win32::OLE->LastError()); #$Word->ActiveDocument->PrintOut({ #Background => 0, #Append => 0, #Range => wdPrintAllDocument, #Item => wdPrintDocumentContent, #Copies => 1, #PageType => wdPrintAllPages,

Replies are listed 'Best First'.
Re: Is it possible to print off Exel and Word documents in perl.
by AppleFritter (Vicar) on Jul 08, 2014 at 20:19 UTC

    You're on the right path there. Sending the files directly to the printer will not work as they are indeed just binary files as far as the printer is concerned; you'll have to talk Word into printing them for you.

    I also see you've already found the sample code in Active State's "Using OLE with Perl" FAQ. Isn't that solution working?

Re: Is it possible to print off Excel and Word documents in perl.
by davies (Monsignor) on Jul 08, 2014 at 22:16 UTC

    Have a look at Cacharbe's answer to Win32::OLE and Excel and the Web. You will have to print from Excel rather than copying the file to a printer and that printer will have to be installed on the computer running Excel. The classic advice is to record a macro that prints the file and then convert that macro to Perl, but be aware that the recorded macro won't be good VBA and that simply transliterating it won't give you good Perl. If you can't print from Excel, then that's the first thing you need to fix.

    Regards,

    John Davies