banduwgs has asked for the wisdom of the Perl Monks concerning the following question:

Hey monks,
I am reading document properties and analyse content of the document via a Perl module. I use "File::Find::finddepth" mothod to travers directories.

I use single word application to read all word documents. I tried it with several options, but came across problems each time. I have commented problem occured in each case. Best one was the first code line even it's not perfect.
#$word_app = Win32::OLE->new('Word.Application', 'Quit(wdDoNotSaveChan +ges)'); # Winword program doesn't terminate #$word_app = Win32::OLE->new('Word.Application', 'Quit'); # Pr +oblem: Ask for save changes for all documents #$word_app = Win32::OLE->new('Word.Application'); # 2 Winword +programs which doesn't terminate. $word_app = Win32::OLE->new('Word.Application', 'Quit(wdDoNotS +aveChanges)');
I open and close documents with following code line:
if ($obj = $word_app->Documents->open($file, 0, 1)) { $obj->Close('wdDoNotSaveChanges'); }
What is the thing go wrong here?
Thanks in advance.

Replies are listed 'Best First'.
Re: OLE word problem
by Anonymous Monk on Jul 07, 2003 at 13:31 UTC

    Could it be that Win32::OLE doesn't know the Word constants? e.g. wdDoNotSaveChanges

    #!C:/ActivePerl/bin/perl.exe -w use strict; use Win32::OLE; my $word_app = Win32::OLE->new("Word.Application") or die ("Didn't create Word App.\n"); my $file="F:/test.doc"; my $word_file = $word_app->Documents->Open($file) or die ("Didn't manage to create a document object.\n"); print "File $file has " . $word_file->Words->Count . " words in it.\n"; $word_file->Close(0); #SaveChanges:=False $word_app->Quit; #SaveChanges not needed as files closed.
    Seems to work fine. (Even if Document.Words.Count isn't what I thought :-) Do ALT+F11 in Word for the VB Help/Designer.
      Thanks for your advice munk.
      Anyhow I am loading word constants within a package as
      use WIN32::OLE::Const 'Microsoft Word';
      The other thing is the constant in close method work perfectly; I checked it. If you have free enough time here's whole code
      ---------------------------------------------------
      ##################### # # Initiating program # ##################### use strict; use LTG::DocBuddy::OfficeSub; use File::Find; require Exporter; use vars qw(@ISA @EXPORT); my $dir = 'C:\sumith\project\test_data\input\word\small'; find(\&parse_office_doc, $dir);
      ---------------------------------------------------
      ##################### # # Office package to handle all # types of office files (but I # simplified it, yet give same # problem # ##################### package LTG::DocBuddy::OfficeSub; use strict; use diagnostics; use WIN32::OLE; use WIN32::OLE::Variant; require Exporter; use vars qw(@ISA @EXPORT); use LTG::DocBuddy::Config qw($DEBUG); use LTG::DocBuddy::WordSub; @ISA = qw(Exporter); @EXPORT = qw(parse_office_doc); $DEBUG && print "Loading LTG::DocBuddy::OfficeSub package...\n"; my ($word_app, $excel_app, $ppt_app, $access_app); $word_app = Win32::OLE->new('Word.Application', 'Quit(0)'); sub parse_office_doc { $DEBUG && print "Calling LTG::DocBuddy::OfficeSub->parse_office_do +c(@_) method...\n"; my $file = shift; my %file_info = (); my $obj; my $whole_text; if ($obj = $word_app->Documents->open($file, 0, 1)) { %file_info = read_doc_properties($obj); $file_info{'Style'} = {get_word_content($obj)}; foreach my $style (keys %{$file_info{'Style'}}) { $whole_text .= ${$file_info{'Style'}}{$style}; } delete $file_info{'Style'}; $obj->Close(0); } if (not $obj) { return { error => "$file: GetObject not successful: ". Win32::OLE->LastError() }; } $file_info{'Whole text'} = $whole_text; return %file_info; } sub read_doc_properties { $DEBUG && print "Calling LTG::DocBuddy::OfficeSub->read_doc_proper +ties(@_) method...\n"; my $obj = shift; my %file_info; if (my $properties = $obj->{BuiltInDocumentProperties}) { foreach my $prop (in $properties) { $file_info{$prop->{'Name'}} = "$prop->{'Value'}" if ($prop +->{'Value'}); } } return %file_info; } 1;
      ---------------------------------------------------
      ##################### # # Word handling package # ##################### package LTG::DocBuddy::WordSub; use strict; use WIN32::OLE; use WIN32::OLE::Variant; use WIN32::OLE::Const 'Microsoft Word'; require Exporter; use vars qw(@ISA @EXPORT); use LTG::DocBuddy::Config qw($DEBUG); @ISA = qw(Exporter); @EXPORT = qw(get_word_content); $DEBUG && print "Loading LTG::DocBuddy::WordSub package...\n"; sub get_word_content { $DEBUG && print "Calling LTG::DocBuddy::WordSub->get_word_content( +@_) method...\n"; my $obj = shift; my $enumerate = new Win32::OLE::Enum($obj->Paragraphs()); my %content; while(defined(my $paragraph = $enumerate->Next())) { $content{$paragraph->{Style}->{NameLocal}} .= "$paragraph->{Ra +nge}->{Text}\n\n"; } $enumerate->Reset(); return %content; } 1;
      ---------------------------------------------------
      Thanks again for your attention and do you have any suggessions?