Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word OLE

Status
Not open for further replies.

afx

Programmer
Mar 4, 2004
27
CA
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
 
I only changed a couple things to test this on my system, but I didn't have any problems with word not shutting down. Have you tried it without running the AutoOpen macro? At least that would tell you if the macro is causing the problem. Also, what version of Word are you using?
(perldoc - Using OLE with Perl)
In the case of Microsoft Office 97, make sure that you have at least updated to Service Release 1 - much of the OLE in Microsoft Office 97 is broken without this update.

I have some code for finding a particular process ID and killing it, but I don't have access to it right now. For now you could try parsing the output from tasklist.exe, then use the kill() function on the process ID.

Are you using this in a subroutine or did you copy it out of one? I noticed you have some return statements in there that don't look like they belong

this is the code I tested with:
Code:
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Word';

my $inputFile = "c:/test.doc";
my $saveFile = "c:/test.rtf";

my $word;
eval{$word = Win32::OLE->GetActiveObject('Word.Application')};
    die 'Word not installed' if $@;
unless (defined $word) {

    $word = Win32::OLE->new('Word.Application', 'Quit')
        or die "Could not open Word";
}

$word->{DisplayAlerts} = wdAlertsNone;

$doc = $word->Documents->Open($inputFile,{Visible => 1})
    or die("Unable to open $inputFile ", Win32::OLE->LastError());

$doc->SaveAs($saveFile, {FileFormat => wdFormatRTF});
$doc->Close;print Win32::OLE->LastError();
$word->Quit;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top