in reply to Manipulating MSWord with Win32::OLE to convert a word document to pdf

Here's some old code which I had lying around. It might do exactly what you're looking for.

use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Word'; use Getopt::Std; my %opts; getopts('o:i:', \%opts); my $infile = $opts{i} || die "No input File specified - use -i file\n +"; my $outfile = $opts{o} || die "No output File specified - use -o file\ +n"; my $Word = Win32::OLE->new('Word.Application', 'Quit'); # $Word->{'Visible'} = 1; # if you want to see what's going on $Word->Documents->Open($infile) || die("Unable to open document ", Win32::OLE->LastError()); $Word->{DisplayAlerts}=-1; $Word->ActiveDocument->PrintOut({ Background => 0, Append => 0, Range => wdPrintAllDocument, Item => wdPrintDocumentContent, Copies => 1, PageType => wdPrintAllPages, OutputFileName => $outfile, PrintToFile => 1, }); $Word->ActiveDocument->Close(0); unlink $outfile; #The pdf creation appends ".pdf" to $outfile, but c +reates an empty $outfile as well

Then again, it might not. I've given up using Adobe Acrobat, and I'm now using Samba and ghostscript to do the same thing for free. See here if you're interested.


davis
It's not easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.
  • Comment on Re: Manipulating MSWord with Win32::OLE to convert a word document to pdf
  • Download Code

Replies are listed 'Best First'.
Re^2: Manipulating MSWord with Win32::OLE to convert a word document to pdf
by kepster (Initiate) on Jan 27, 2006 at 15:55 UTC
    Hi, I've been using a slightly modified verison of this code, the problem I'm having is when Word tries to print Acrobat Distiller produces an error: When you create a postscript file you have to send the host fonts. I can change this property manually when running word, but I can't find how to automate it, any ideas folks?

      I wouldn't use Samba anymore. I'd use PDFCreator now, and this message looks to point to a very easily scriptable method.
      cheers


      davis
      Kids, you tried your hardest, and you failed miserably. The lesson is: Never try.