Solved

# How to print (on Windows ) from perl

Posted on 2004-11-03
177 Views
Hi gurus -

I have a program that reads a mysql database and outputs a spreadsheet -

Is there a way to get it to print off to the default printer automatically -

Or maybe write out the file - then get a 2nd program to read the directory and if there is a document , then print it out .....

Can you give me a few pointers ?

Rab
0
Question by:rab54

LVL 19

Expert Comment

Which spreadsheet format are you going to output it to? Of course you need to tell us, your OS platform too.

On windows you can simple shell out the document with a '/P' switch. Generally, on Windows platform that switch causes the host application to print it using the default printer.

0

LVL 19

Expert Comment

Oh my!! Sorry, i see that you wrote "print (on Windows ) from "...

Need galsses????
0

Author Comment

I have used the system command within perl to print a test file

eg - system "type $file >$printer";

This works ok until I try a spreadsheet - doh !

I just need a dos command to print say -

c:/test.xls

I had no joy with PRINT and /P either .....

cheers
0

LVL 19

Expert Comment

Nope. Try this:

cmd start %1 /p
0

Author Comment

Can you tell me what this actually does ?

eg 'start' ,%1 /p etc ...

sorry to be numb ...
0

LVL 19

Expert Comment

cmd is the command line processor on Windows NT. Its equivalent on Windows 9x is Command.com Its kind of a shell program. It can be used to execute other programs as well.

'Start' is a means to launch an executable file or a document file with the help of an application registered to handle that particular document type.

/P is a switch generally used by Windows applications to quitely print a document.

Hope that helps!
0

Author Comment

ok i am trying this within my perl program -

system "cmd start perl.exe $file/P"; But still having no joy - Does the above mean use perl to run the program in$file ?

But $file is a spreadsheet .... not a perl program - Thanks for the continued help .... I feel like a blindman in the dark ..... ;~) 0 Author Comment OK cheers guys - Got it sorted using Win32::OLE rab 0 LVL 19 Expert Comment Its good that you found a solution. Can you please post your relevent code for us. 0 Author Comment No problem mate - If you see the code below - it reads in the selected spreadsheet - we get the default printer using the ActivePrinter method - assign it and bingo ! There is an option to output to another file if needed - Hope this is of help to others - cheers Rab ps - Obviously you will need to install the Win32::OLE module first ;~) ################### code #########################$Win32::OLE::Warn = 3;                                # die on errors...

use Win32::OLE qw(in);
use Win32::OLE::Const;
my $Class = "Excel.Application"; my$File  = "path\to_file";

my $Excel = Win32::OLE->GetActiveObject($Class );
if (!($Excel)) {$Excel = new Win32::OLE($Class, \&QuitApp ) || die "Could not cretate an OLE '$Class' object";
}
$Excel->{Visible} = 1; # my$Book = $Excel->Workbooks->Open($File ); # open the file
#    my $count =$Book->Worksheets()->{Count};
#    print("There's $count worksheets in$File\n\n");

my $Book =$Excel->Workbooks->Open( $File); # above line opens the file print("Opened file$File...\n");
if (!($Book)) { print("Error during open, filename=$File. returning 0.");
return 0;
}

my $count =$Book->Worksheets()->{Count} || 0;

my $pobj =$Excel->ActivePrinter;

## check for correct printer
if ($pobj) { print("Name of default printer =>>$pobj<<\n"); }

$Book->ActiveSheet->Activate(); my @thing =$Book->PrintOut(
{
#   PrintToFile   => 1,
# PrToFileName  => "$outfile", # PrToFileName => "$pobj",
ActivePrinter => "\$pobj",
Copies        => 1
});

################# end code ########################

0

LVL 19

Expert Comment

Your code looks okay. Just that it only prints Excel files. For your question it seemed you wanted to use shell to print *ANY* file in a generic way. Anyway, glad for you that you solved your probolem.
0

Accepted Solution

PAQed with points refunded (125)

modulo
Community Support Moderator
0

## Featured Post

INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …