in reply to Re: OLE word problem
in thread OLE word problem
The other thing is the constant in close method work perfectly; I checked it. If you have free enough time here's whole codeuse WIN32::OLE::Const 'Microsoft Word';
---------------------------------------------------##################### # # 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;
|
|---|