Dennis W. Ruffner, Programmer, PHP, MySQL, ODBC, perl, VB for Access

   September 05, 2010
12:45 a.m.
  
aaconvert.pl -- convert from Word to text format
#!/usr/bin/perl -w

#///////////////////////////////////////////////////
#// Dennis W. Ruffner
#// December 2004
#// aaconvert.pl: convert from Word to text format
#// 888 Web Gurus
#///////////////////////////////////////////////////
#// Note: adapted from a program origionally intended to create html documents
#// http://aspn.activestate.com/ASPN/Mail/Message/perl-win32-web/2284880
#//
#// Can run with by itself with input and output files specified as indicated under Example below
#// or can be called by aasys.pl which walks a directory path.
#
# Example:
# perl test_msword.pl C:/temp/dok.doc C:/temp/

print "aa6.pl\n";

use strict; use warnings;

use Win32;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Word';

use vars qw( %const $wd );

my $document = shift;
my $outpath = shift;

$wd = Win32::OLE::Const-> Load(".*Word.*");
%const = %{ $wd };

my $ex = Win32::OLE-> GetActiveObject('Word.Application');
if (Win32::OLE-> LastError()) {
     print Win32::OLE-> LastError() . "\n";
}

unless( defined( $ex ) ) {
     # $ex = Win32::OLE-> new('Word.Application', sub {$_[0]->Quit;});
     $ex = Win32::OLE-> new( 'Word.Application', 'Quit' );
}

unless( defined( $ex ) ) {
     if (Win32::OLE-> LastError()) {
         print 'Error starting MSWord: ' . Win32::OLE-> LastError() .
"\n";
     }
     exit();
}
my $doc = '';

$doc = $ex-> Documents->Open( {
         FileName => $document,
         ConfirmConversions => 0,
         ReadOnly => 1,
         AddToRecentFiles => 0,
         PasswordDocument => '',
         PasswordTemplate => '',
         Revert => 0,
         WritePasswordDocument => '',
         WritePasswordTemplate => '',
         Format => $const{'wdOpenFormatAuto'},
         Visible => 1,
         } );

unless( defined( $doc ) || Win32::OLE-> LastError() ) {
     print "Error trying to open '$document':" . Win32::OLE-> LastError()
. "\n";
     $ex-> Quit();
     exit();
}

$doc-> WebOptions->{OrganizeInFolder} = 0;

my $html_document = $outpath;
$doc-> SaveAs( { FileName => $html_document,
         FileFormat => $const{'wdFormatDOSTextLineBreaks'} } );
if( Win32::OLE-> LastError() ) {
     print $document . ' Error saving: ' . Win32::OLE-> LastError() .
"\n";

     $doc->Close(0);
     $doc->Quit();
     exit();
}

print "OK!\n";

This page has been visited 1269 times.