use strict; # import OLE use Win32::OLE qw(in with); use Win32::OLE::Const; use Win32::OLE::Const 'Microsoft Word'; $Win32::OLE::Warn = 3; # report OLE runtime errors my $pickupDir = 'C:\\p2exe\\word2txt'; my @ads = getAdNums(); collectAds(@ads); #For the time being, assume that a list of ad numbers will be gathered as an array. sub getAdNums { my @AdNums = ("test"); return @AdNums; } ###################################################### # Read in word files and export as text docs ###################################################### sub collectAds { foreach my $ad (@_) { # Need docname and savename my $doc = "$pickupDir\\$ad.doc"; my $savename = "$pickupDir\\$ad.txt"; # Add check to make sure .doc file exists if (-e $doc) { wordToText($doc, $savename); my @info = get_file($savename); printArr(@info); } } } sub wordToText { my $input = shift; my $output = shift; print "Starting word\n"; my $word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application', 'Quit'); $word->{Visible}= 0; # we don't need to see Word in an active window # open the specified Word doc print "Opening $input\n"; $word->Documents->Open($input) or die("Unable to open document ", Win32::OLE->LastError()); # save the file as a text file $word->ActiveDocument->SaveAs({ FileName => $output, FileFormat => wdFormatDOSTextLineBreaks}); # close document and Word instance print "Closing document and Word\n"; $word->ActiveDocument->Close(); $word->Quit; } ######################################################## # Standard subs below # Need to redo to get rid of unreferenced Scalar. sub get_file { my $file = shift; local *IN; open (IN, "<$file") or die "Cannot read '$file': $!"; if (wantarray) { return ; } else { return join '', ; } } sub printArr { my $n = 0; foreach (@_) {print $n++.": $_\n"}; } #### Attempt to free unreferenced scalar at C:\p2exe\word2txt\debug_word2txt.pl line 28. Can't call method "Dispatch" on unblessed reference at (eval 1) line 1. END failed--call queue aborted. #### use strict; # import OLE use Win32::OLE qw(in with); use Win32::OLE::Const; use Win32::OLE::Const 'Microsoft Word'; $Win32::OLE::Warn = 3; # report OLE runtime errors my $pickupDir = 'C:\\p2exe\\word2txt'; my @ads = getAdNums(); collectAds(@ads); #For the time being, assume that a list of ad numbers will be gathered as an array. sub getAdNums { my @AdNums = ("test"); return @AdNums; } ###################################################### # Read in word files and export as text docs ###################################################### sub collectAds { foreach my $ad (@_) { # Need docname and savename my $doc = "$pickupDir\\$ad.doc"; my $savename = "$pickupDir\\$ad.txt"; # Add check to make sure .doc file exists if (-e $doc) { print "Starting word\n"; my $word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application', 'Quit'); $word->{Visible}= 0; # we don't need to see Word in an active window # open the specified Word doc print "Opening $doc\n"; $word->Documents->Open($doc) or die("Unable to open document ", Win32::OLE->LastError()); # save the file as a text file $word->ActiveDocument->SaveAs({ FileName => \$savename, FileFormat => wdFormatDOSTextLineBreaks}); # close document and Word instance print "Closing document and Word\n"; $word->ActiveDocument->Close(); $word->Quit; my @info = get_file($savename); printArr(@info); } } } ######################################################## # Standard subs below # Need to redo to get rid of unreferenced Scalar. sub get_file { my $file = shift; local *IN; open (IN, "<$file") or die "Cannot read '$file': $!"; if (wantarray) { return ; } else { return join '', ; } } sub printArr { my $n = 0; foreach (@_) {print $n++.": $_\n"}; }