Monthly Archive

Categories

Word Add a table

There seems to be two types of paragraph I create in Word documents – text or tables.  Lets start by adding a table.

001
002
003
004
005
006
007
008
009
function Add-Table {
    param (
        [int] $row = 2,
        [int] $col = 5
    )
    $global:paragraph = $doc.Content.Paragraphs.Add()
    $range = $paragraph.Range
    $global:table = $doc.Tables.Add($range,$row,$col)
}

 

The function Add-Table takes two integers as parameters. They define the rows and columns of the table.  The way that seems to work best is to add a paragraph to contain the table and then add the table into the paragraph.  If we just add the table to the document it overwrites the contents of the document with the table. oops.

Notice that I’m using global variables again so that they can be used across the environment.

Next time we will add some data to the table.

Technorati Tags: Powershell v2,modules,Word,table

One Response to Word Add a table

  • Carl Webster says:

    How can I select a table so I can indent it?

    Word doesn’t let me record selecting a table but it does let me record clicking the “Increase Indent” button three times.

    Selection.Paragraphs.Indent
    Selection.Paragraphs.Indent
    Selection.Paragraphs.Indent

    Is what happens. But how do I select the table first?

Leave a Reply