Question

I am parsing a word 2007 using powershell. I would like get each table's caption and print it to screen.

Here is an idea of what I would like to do, but the print caption line does not work.

$wd = New-Object -ComObject Word.Application
$wd.Visible = $true
$doc = $wd.Documents.Open($filename)
foreach ($table in $doc.Tables)
{
      #print table caption
      Write-Host $table.Caption.Range.Text #This does not work

      #Print Table contents
      foreach ($row in $table.Rows)
      {
          Write-Host $row.Range.Text
      }
}

Is it possible to get the caption associated with a table?

Was it helpful?

Solution

Captions aren't properties of table objects, they're just text. You could do something like this for finding table captions, though:

$style = $doc.Styles | ? { $_.NameLocal -eq 'Caption' }
$wd.Selection.Find.Style = $style
$wd.Selection.Find.Text  = 'Table'  # if table captions are labeled "Table"
$wd.Selection.Find.Execute()

Since wildcard matches are non-greedy in Word, you may have to do something like this:

$style = $doc.Styles | ? { $_.NameLocal -eq 'Caption' }
$wd.Selection.Find.Style = $style
$wd.Selection.Find.Wrap  = 0
while ($wd.Selection.Find.Execute()) {
  if ($wd.Selection.Text -like 'Table*') {
    # do stuff
  }
  $wd.Selection.MoveRight()
}

The above matches just by style, so it should select the entire caption. The MoveRight() unselects the caption afterwards, so the next iteration will find the next match until the end of the document, where the search stops (.Find.Wrap = 0).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top