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

Lets say there are several exactly looking tables in the document and I need data from a specific one of them. I don't know the text inside the table (thats what I'm after) so I can't itererate through all tables and cells and look for the right one. But, I do know exact text before it (it's the name of the section).

How can I select the table I need or figure out which table index (number for ActiveDocument->Tables(#) in the code below)?

use v5.10; use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; $Win32::OLE::Warn = 3; # die on errors... my $Word = Win32::OLE->new('Word.Application', 'Quit'); $Word->{'Visible'} = 1; my $document = $Word->Documents->Open('D:\path\tmp.doc'); say "Could not open doc:". Win32::OLE->LastError() if !$document ; say $Word->ActiveDocument->Tables->Count; my $x = $Word->ActiveDocument->Tables(5)->Cell(2,1)->Range->Text; say $x;

Replies are listed 'Best First'.
Re: Finding the right table in MS Word document [Win32::OLE]
by Anonymous Monk on Jun 04, 2010 at 14:21 UTC
    First you search for text (Select?), after you find it, you use Next method to find objects after your selection until you run into a table..... probably easier to create a macro and translate it.

    Very similar to HTML DOM programming