I am new to win32:ole module in perl. I am trying to print MS word table data row wise on command prompt. But I am able to print only last row of the table. Can you please help me to solve this problem? Thanks in advance.
Below is my code:
#!/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('C:\\PerlScripts\\myTest.docx');
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;
}
eval { ... }
when you're just going todie "$@\n" if $@
afterward anyway. Is there? – Cauldronuse strict
isn't in effect, it's possible that thewdSeparateByTabs
is being treated as the string'wdSeparateByTabs'
, and similarly for other constants. (I mean, obviously the real issue in that case would be thatuse Win32::OLE::Const 'Microsoft Word'
is not loading a constant that the OP expects it to; but the lack ofuse strict
could conceal that issue.) – Cauldron