I have some code that opens Word (which then runs the AutoOpen macro which is programmed to run some VB code to convert math equations to XML) then does a SaveAs to an RTF file.
This works fine - ONCE!!
When I run the same code again - it doesn't do anything (that I can see). I changed the code to do a $word->DESTROY() but when I check the Processes that are running WINWORD is still there.
If I kill WINWORD then run my code again - it works ONCE!
The code follows:
use Win32::OLE;
# use Win32::OLE::Const 'Microsoft Word';
my($word);
eval{$word = Win32::OLE->GetActiveObject('Word.Application')};
return('Word not installed') if $@;
unless (defined $word) {
$word = Win32::OLE->new('Word.Application', sub {$_[0]->Quit;})
or return('Could not open Word');
}
$infile =~ s|\\|/|g;
$infile =~ m"^(.*)/(.*)$";
my($filepath) = $1;
my($filebase) = $2;
$filebase =~ m/^(.*)\.(.*)$/;
$filebase = $1;
my($openfile) = $infile;
my($savefile) = "$filepath\\$filebase" . ".rtf";
if (-e "$infile") {
$word->{DisplayAlerts} = wdAlertsNone;
my($doc) = $word->Documents->Open($openfile, {Visible => 0})
or return("Cannot open input document" . Win32::OLE->LastError);
# MathType equation conversion will be executed as a Word Macro "AutoOpen"
# therefore, all equations should be converted upon opening Word file.
$doc->SaveAs($savefile, {FileFormat => wdFormatRTF});
$doc->Close;
$word->Quit;
$word->DESTROY();
undef($doc);
undef($word);
} else {
return("Could not find $infile");
}
Any ideas how to kill Word using Perl or Perl/OLE?
regards
Mark H., AFX
This works fine - ONCE!!
When I run the same code again - it doesn't do anything (that I can see). I changed the code to do a $word->DESTROY() but when I check the Processes that are running WINWORD is still there.
If I kill WINWORD then run my code again - it works ONCE!
The code follows:
use Win32::OLE;
# use Win32::OLE::Const 'Microsoft Word';
my($word);
eval{$word = Win32::OLE->GetActiveObject('Word.Application')};
return('Word not installed') if $@;
unless (defined $word) {
$word = Win32::OLE->new('Word.Application', sub {$_[0]->Quit;})
or return('Could not open Word');
}
$infile =~ s|\\|/|g;
$infile =~ m"^(.*)/(.*)$";
my($filepath) = $1;
my($filebase) = $2;
$filebase =~ m/^(.*)\.(.*)$/;
$filebase = $1;
my($openfile) = $infile;
my($savefile) = "$filepath\\$filebase" . ".rtf";
if (-e "$infile") {
$word->{DisplayAlerts} = wdAlertsNone;
my($doc) = $word->Documents->Open($openfile, {Visible => 0})
or return("Cannot open input document" . Win32::OLE->LastError);
# MathType equation conversion will be executed as a Word Macro "AutoOpen"
# therefore, all equations should be converted upon opening Word file.
$doc->SaveAs($savefile, {FileFormat => wdFormatRTF});
$doc->Close;
$word->Quit;
$word->DESTROY();
undef($doc);
undef($word);
} else {
return("Could not find $infile");
}
Any ideas how to kill Word using Perl or Perl/OLE?
regards
Mark H., AFX