Script to convert Word to Postscript



I needed to convert lots of Word documents to PDF. This script automates the first step, going from Word to Postscript.

use Win32;
use Win32::API;
use Win32::OLE;
use strict;
use Win32::OLE qw(in);
use Win32::OLE::Const 'Microsoft Office';
use Win32::OLE::Const 'Microsoft Word';
sub word2ps {
	my $docfile = shift;
	my $psfile = shift;
	my $word = Win32::OLE->new('Word.Application', 'Quit')
		or die Win32::OLE->LastError;
	my $doc = $word->Documents->Add({
		'Template' => $docfile,
		}) or die Win32::OLE->LastError;
	my $orgback = $word->Options->{PrintBackground};
	my $orgprinter = $word->ActivePrinter;
	$word->Options->{PrintBackground} = 1;
	my $printer = 'Adobe PDF';
	$word->{ActivePrinter} = $printer;
	$doc->Activate;
	$word->PrintOut({
		'Range' => wdPrintAllDocument,
		'PrintToFile' => 1,
		'OutputFileName' => $psfile,
		'Copies' => 1
		});
	for (my $i = 0; $i < 600; $i++) {
		sleep 1;
		last unless $word->{BackgroundPrintingStatus};
	}
	$word->Options->{PrintBackground} = $orgback;
	$word->{ActivePrinter} = $orgprinter;
	$word->Quit({
		'SaveChanges' => wdDoNotSaveChanges,
		});
}

One Response to “Script to convert Word to Postscript”

  1. Manodev says:

    How does this script work? How do you give the inputs to this script?

Leave a Reply