locked
Table formatting in a Word document created with PowerShell RRS feed

  • Question

  • I am using Powershell to create a Word 2010 document that contains a table.  Everything is working great, except that I want to change the height of the rows to eliminate the white space that is currently there.  I've tried different table styles using $table.AutoFormat(<style>).  I've also tried setting $table.AllowAutoFit and $table.Rows.HeightRule.  So far nothing has worked.  If  I open the document in Word after it is created and select any table style, the row height automatically shrinks down to how I would like it to be.  Any ideas on how I can get the row height to change from the PowerShell script?  Thanks!

    Here's the part of my script that creates the Word document & table:

    Here is how the table looks:

    I want to get rid of the extra space between the rows.

    Wednesday, December 19, 2012 7:54 PM

Answers

  • After creating and filling the table, do this:

    $table.UpdateAutoFormat()

    That should correctly resize the table vertically.

    • Proposed as answer by Alex K. Angelopoulos Sunday, December 23, 2012 6:48 AM
    • Marked as answer by Yan Li_ Wednesday, December 26, 2012 1:50 AM
    Friday, December 21, 2012 4:26 AM

All replies

  • Would you be able to post your script and the Word data as actual text rather than pictures? It makes it simpler for people to replicate your scenario and provide possible solutions. :)

    Thursday, December 20, 2012 7:41 AM
  • Sure - I should have thought of that!  :)  The script below generates a word doc with a table.  When I view the Word document, it looks like there is a blank line under the text in each cell.  Thanks!

    $filename = "C:\test.docx"
    $countOfRows = "4"
    $countOfColumns = "3"
    $values = @("field1","field2","field3","field4","field5","field6","field7","field8","field9","field10","field11","field12")

    $word = New-Object -ComObject "Word.Application"
    $doc = $word.documents.Add()
    $word.visible = $True
    $selection = $word.Selection
    $range = $selection.Range
    $table = $doc.Tables.add($range,$countOfRows,$countOfColumns)
      
    for ($r=1; $r -le $countOfRows; $r++) {
     for ($c=1; $c -le $countOfColumns; $c++) {
         $tempr = $r - 1
      $tempc = $c - 1
        $line = $values[$tempr + $tempc*$countOfRows]
         $table.cell($r,$c).range.text = $line
     }
    }

    $doc.SaveAs([ref]$fileName)
    $doc.Close()
    $word.quit()

    Thursday, December 20, 2012 2:06 PM
  • After creating and filling the table, do this:

    $table.UpdateAutoFormat()

    That should correctly resize the table vertically.

    • Proposed as answer by Alex K. Angelopoulos Sunday, December 23, 2012 6:48 AM
    • Marked as answer by Yan Li_ Wednesday, December 26, 2012 1:50 AM
    Friday, December 21, 2012 4:26 AM
  • I am afraid the $table.UpdateAutoFormat() is not working for me.  It resizes the table horizontally, but not vertically.
    Wednesday, December 26, 2012 2:20 PM
  • Adding the following to set the table style and remove the borders formatted the table the way I wanted:

      $table.Range.Style = "Table Grid"
      $table.Borders.InsideLineStyle = 0
      $table.Borders.OutsideLineStyle = 0

    Thanks!

    Thursday, December 27, 2012 2:22 PM
  • Your method always works for me as well, so it seems to be more general.

    Friday, December 28, 2012 5:34 PM