For general maintainability, I recommend avoiding $word->ActiveDocument. If you'll only be working with the current document, then do my $doc = $word->ActiveDocument somewhere at the top of your code.
Going from this Stackoverflow answer, you could use $doc->Selection->GoTo as suggested in the Microsoft documentation:
for my $para (1..$doc->Paragraphs->Count) {
$doc->Selection->GoTo( What => wdGoToParagraph, Which => wdGoToAbs
+olute, Count => $i );
my $para = $doc->Selection->Paragraphs->[0];
$doc->Selection->GoTo( What => wdGoToTable, Which => wdGoToNext );
my $table = $doc->Selection->Tables->[0];
...
}
This will still trip for paragraphs that have no table. Maybe in that case, it makes sense to step through all tables instead and then go backwards to find the corresponding paragraph or heading, depending on how your document is structured. |