Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Word 2007/2010/2013 Add a Caption to Table and Move Table 3 Tabs to Right

PowerShell V2 Word 2007/2010/2013

I have created a test script the inserts a Word table and populates it with no issues.

There are two more things I would like to do:

Add a caption above the table that says "Table x Citrix Services" Move the table to the right three tab stops.

Sample document can be found at https://dl.dropbox.com/u/43555945/StackOverflowSample.docx

Here is my test code:

$wdSeekPrimaryFooter = 4
$wdAlignPageNumberRight = 2
$wdStory = 6
$wdMove = 0
$wdSeekMainDocument = 0
$wdColorGray15 = 14277081
$wdCaptionPositionAbove = 0

$Word = New-Object -comobject "Word.Application"
$Word.Visible = $True
$word.Templates.LoadBuildingBlocks()
$BuildingBlocks=$word.Templates | Where {$_.name -eq "Built-In Building Blocks.dotx"}
$part=$BuildingBlocks.BuildingBlockEntries.Item("Motion")

$Doc = $Word.Documents.Add()
$Selection = $Word.Selection

$part.Insert($selection.Range,$True) | out-null
$selection.InsertNewPage()

#table of contents
$toc=$BuildingBlocks.BuildingBlockEntries.Item("Automatic Table 2")
$toc.insert($selection.Range,$True) | out-null

#set the footer
[string]$footertext="Report created by Webster"

#get the footer
$doc.ActiveWindow.ActivePane.view.SeekView=$wdSeekPrimaryFooter
#get the footer and format font
$footers=$doc.Sections.Last.Footers
foreach ($footer in $footers) 
{
    if ($footer.exists) 
    {
        $footer.range.Font.name="Calibri"
        $footer.range.Font.size=8
        $footer.range.Font.Italic=$True
        $footer.range.Font.Bold=$True
    }
} #end Foreach

$selection.HeaderFooter.Range.Text=$footerText

#add page numbering
$selection.HeaderFooter.PageNumbers.Add($wdAlignPageNumberRight) | Out-Null

#return focus to main document
$doc.ActiveWindow.ActivePane.view.SeekView=$wdSeekMainDocument

#move to the end of the current document
$selection.EndKey($wdStory,$wdMove) | Out-Null

$services = get-service | where-object {$_.DisplayName -like "*Citrix*"} | sort-object DisplayName

$TableRange = $doc.application.selection.range
$Columns = 2
$Rows = $services.count + 1
$Table = $doc.Tables.Add($TableRange, $Rows, $Columns)
$table.AutoFitBehavior(1)
$table.Style = "Table Grid"
$table.Borders.InsideLineStyle = 1
$table.Borders.OutsideLineStyle = 1
$xRow = 1
$Table.Cell($xRow,1).Shading.BackgroundPatternColor = $wdColorGray15
$Table.Cell($xRow,1).Range.Font.Bold = $True
$Table.Cell($xRow,1).Range.Text = "Display Name"
$Table.Cell($xRow,2).Shading.BackgroundPatternColor = $wdColorGray15
$Table.Cell($xRow,2).Range.Font.Bold = $True
$Table.Cell($xRow,2).Range.Text = "Status"
ForEach($Service in $Services)
{
    $xRow++
    $Table.Cell($xRow,1).Range.Text = $Service.DisplayName
    $Table.Cell($xRow,2).Range.Text = $Service.Status
}
like image 337
Webster Avatar asked Dec 20 '25 01:12

Webster


1 Answers

Use $Selection.InsertCaption(-2, "Citrix Services") to create a table title by placing that code above your foreach loop that builds the rows.
The first argument is the caption type; -2 is Table other built in labels accessible by negative integers are figure and equation.

Use rows.setleftindent() to indent the table to your desired position. The first argument is the indent distance the second defines the rulerstyle. Move your autofitbehavior instance after the indent to ensure the table autofits correctly.

Add the following lines to your code after the foreach loop that builds the table rows.

$Table.Rows.SetLeftIndent(200,1)
$table.AutoFitBehavior(1)

Hope this helps!

like image 164
runaboutfence Avatar answered Dec 23 '25 23:12

runaboutfence



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!