# 
#   This script will scan for MS Word files, through a specified path,
#   and print them to the default printer.
# 
#   This code should be modified to keep the OLE server alive (as opposed
#   to killing it every time it is needed).  Doing so will make this script
#   run much faster.
# 
use strict;

my ($FileSpec) = "\.doc";
my ($Path) =  "F:\\";
my ($iCounter) = 0;

print "This script will print each and every MS WORD file found...\n";
FindFiles($Path);
print "Total file(s) printed: $iCounter\n";
exit;

sub FindFiles {
    my ($Dir) = @_;
    my (@Dirs, @List, $File);
    $Dir =~ s/\\\\/\\/g if (substr($Dir, 3, 1) eq "\\");
    if (opendir (DIR, $Dir)) {
        @List = readdir (DIR);
        closedir (DIR);
        foreach $File (@List) {
            next if ($File eq "." || $File eq "..");
            push( @Dirs, $File ) if ( -d "$Dir\\$File");
            print ".";

            PrintMSWordFile ("$Dir\\$File") if ($File =~ /($FileSpec)$/i);
        }
        foreach $File (@Dirs) {
            FindFiles("$Dir\\$File");
        }
    }
    print "\n";
}

sub PrintMSWordFile {
    use Win32::OLE;
    use Win32::OLE::Const 'Microsoft Word';

    my ($Filename) = @_;
    $Filename = Win32::GetFullPathName($Filename);

    my $Word = Win32::OLE->new('Word.Application', 'Quit');
    $Word->{'Visible'} = 0;

    $Word->Documents->Open($Filename);
    $Word->ActiveDocument->PrintOut({
        Background     => 0,
        Range          => wdPrintAllDocument,
        Item           => wdPrintDocumentContent,
        PageType       => wdPrintAllPages,
    });
    sleep 5;
    $Word->Quit();
    $iCounter++;
}

-----Original Message-----
From: James E Keenan [mailto:[EMAIL PROTECTED]]
Sent: Monday, February 05, 2001 5:14 PM
To: Perl-Win32-Users
Subject: Printing Files via Microsoft Word


Can someone point me in the direction of learning how to solve this problem?
This weekend I was working a temp job as a word processor in a law firm.  My
assignment:  use Microsoft Word to print out over 500 files from a CD-ROM --
one after the other.  Surely, I thought, if they had Perl loaded on their
system I could have written a quick program to say "Use Word to print all
files in this directory; as you do so make a log of all files so printed."
But, alas, they did not have Perl.  And even if they did, I'm not yet
familiar with the Win32 modules enough to know how to just toss this off.  I
took a look at Win32::OLE, but the coding is quite formidable and the
examples mainly referred to Excel.

Suggestions anyone?  Thanks in advance.

_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users

Reply via email to