Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re:(Win32::OLE) Printing from Perl in Windows

by tachyon (Chancellor)
on Feb 17, 2003 at 05:14 UTC ( #235891=note: print w/replies, xml ) Need Help??

in reply to Printing from Perl in Windows

KISS I reckon. If Word can do it then why not save yourself the pain and just script it....

use Win32::OLE; # start Word program die if unable to $word = Win32::OLE->new('Word.Application', sub { $_[0]->Quit; } ) or die 'Cannot start Word'; # let's watch $word->{'Visible'} = 1; # Create new document my $d = $word->Documents->Add; # define selection my $s = $word->Selection; my @lines = ( "This is test line 1", "This is test line 2", "This is test line 3", ); # $c is the color # $start is the start of Range # $end is the end of Range # $r is the Range object my ($c, $start, $end, $r) = (2, 0, 0, ); for my $line (@lines) { $end += length($line) + 1; $s->TypeText($line); # define the Range $r = $d->Range($start, $end); # Set font properties $r->Font->{Size} = 18; $r->Font->{ColorIndex} = $c++; $r->Font->{Name} = 'Courier New'; $s->TypeText("\n"); $start = $end; } # TIMTOWTDI but this will overwrite the above #my $r = $doc->{Content}; #$r->{Text} = 'Hello World!'; #$r->InsertParagraphAfter(); #$r->InsertParagraphAfter(); #$r->InsertAfter('Bye!'); # here is how to print a document $word->ActiveDocument->PrintOut({ Background => 0, Append => 0, Range => wdPrintAllDocument, Item => wdPrintDocumentContent, Copies => 1, PageType => wdPrintAllPages, }); # save the file without a prompt $word->WordBasic->FileSaveAs("c:\\test.doc"); # have a quick look at the objects and properties # don't try to Dumper the whole thing unless you have time and memory+ ++ print "Range: $_ => $r->{$_}\n" for sort keys %$r; print "Doc: $_ => $d->{$_}\n" for sort keys %$d; # house keeping, clean up our instances $d->Close(); $word->Quit(); undef $word;




Replies are listed 'Best First'.
Re: Re:(Win32::OLE) Printing from Perl in Windows
by John M. Dlugosz (Monsignor) on Feb 17, 2003 at 06:41 UTC
    Very nice—that's most of what I need in your sample.

    I did some Word scripting before, for my C++ Color Pretty Printer. But it has a problem: if I insert too much, it gets in a bad mood. I don't remember the details, but it basically doesn't work any more. The work-around is to Save every few lines of inserting, which doesn't do wonders for its already slow speed.

    It's probably the simplest thing for a directory-listing label, though. I think I'll start with your code.

    Though the Excel idea is also pretty nice, since the data is tabular.


      Just a (possibly irrelvant) thought: Please don't make WinWord/Office a requirement if you're going to distribute this script.


Re: Re:(Win32::OLE) Printing from Perl in Windows
by grantm (Parson) on Feb 17, 2003 at 17:55 UTC

    Perhaps an even simpler approach would be to output HTML/CSS to a file and then automate IE to open and print the file. Word could be used with HTML too, but the results should be more accurate using IE.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://235891]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2020-12-01 06:09 GMT
Find Nodes?
    Voting Booth?

    No recent polls found