UPDATED:
Well, it's not perfect, but by breaking out the "Word" portion to it's own subroutine, I was able to make this work.
I'm guessing somehow two variables were stomping on each other...
Final code:
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') || Win3
+2::OLE->new('Word.Application', 'Quit');
$word->{Visible}= 0; # we don't need to see Word in an active wind
+ow
# 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 <IN>;
} else {
return join '', <IN>;
}
}
sub printArr {
my $n = 0;
foreach (@_) {print $n++.": $_\n"};
}
Final output:
Starting word
Opening C:\p2exe\word2txt\test.doc
Closing document and Word
0: test
I have changed some of the code to make it easier for others to follow along...
I am trying to change word docs to text...
The output I get says:
Attempt to free unreferenced scalar at C:\p2exe\word2txt\debug_word2tx
+t.pl line 28.
Can't call method "Dispatch" on unblessed reference at (eval 1) line 1
+.
END failed--call queue aborted.
Line 28 is where the if clause starts checking the existence of the doc I am trying to convert. if (-e $doc) { I started testing chunks of code, and the word conversion works up until the point where I read in the text document with my "get_file" routine. (AKA if I comment out that line I get no error) But that is the same "get_file" routine I have used for years... And reading in a text doc without doing the Word part works fine...
I am stumped... I have tried changing variables, etc. Can anyone point me as to what I'm doing wrong? Thanks in advance for any help.
Code is below... "Test.doc" is a Word 2003 doc with just the word "test" in it.
Current error:
Attempt to free unreferenced scalar: SV 0x1ab2744, Perl interpreter: 0x2243f4 at C:\p2exe\word2txt\debug_word2txt.pl line 28.
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 act
+ive window
# open the specified Word doc
print "Opening $doc\n";
$word->Documents->Open($doc)
or die("Unable to open document ", Win32::OLE->LastErr
+or());
# 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 <IN>;
} else {
return join '', <IN>;
}
}
sub printArr {
my $n = 0;
foreach (@_) {print $n++.": $_\n"};
}
Commenting out the "get_file" call outputs:
Starting word
Opening C:\p2exe\word2txt\test.doc
Closing document and Word
Commenting out the Word portion outputs:
0: test
Running the whole thing gives me:
Attempt to free unreferenced scalar: SV 0x1ab2744, Perl interpreter: 0x2243f4 at C:\p2exe\word2txt\debug_word2txt.pl line 28.