I need to fetch tabular data from MS-Word file. The code that I referred to only fetches the first and last rows, but I need to fetch the entire table.
Later, the data that was fetched has to be cross-checked if the same filename exists in the folder.
I am not even able to understand the flow of the code, as I am new to the Win32::OLE
module.
I have referred to a similar question about fetching data on this site, but couldn't get it.
Please let me know how to proceed.
#!/usr/bin/perl
use strict;
use warnings;
use File::Spec::Functions qw( catfile );
use Win32::OLE qw(in);
use Win32::OLE::Const 'Microsoft Word';
$Win32::OLE::Warn = 3;
my $word = get_word();
$word->{DisplayAlerts} = wdAlertsNone;
$word->{Visible} = 1;
my $doc = $word->{Documents}->Open('D:\A.doc');
my $tables = $word->ActiveDocument->{'Tables'};
for my $table (in $tables) {
my $tableText = $table->ConvertToText({ Separator => wdSeparateByTabs });
print "Table: " . $tableText->Text() . "\n";
}
$doc->Close(0);
sub get_word {
my $word;
eval { $word = Win32::OLE->GetActiveObject('Word.Application'); };
die "$@\n" if $@;
unless (defined $word) {
$word = Win32::OLE->new('Word.Application', sub { $_[0]->Quit })
or die "Oops, cannot start Word: ", Win32::OLE->LastError, "\n";
}
return $word;
}
UPDATE: A.doc
Article No. Count No Committee
A0029 A0029 16 E01.07
B0028 B0028 34 E04.09
C0036 C0036 17 E09.00
D0033 D0033 15 E08.07
Output in CMD
D:\Word>A.pl
D0033 D0033 15 E08.07No Committee