PowerShell Word 2007/2010/2013 向表格添加标题并将表格 3 选项卡移至右侧

标签 powershell ms-word

PowerShell V2 Word 2007/2010/2013

我创建了一个测试脚本,用于插入 Word 表格并毫无问题地填充它。

还有两件事我想做:

在表格上方添加标题“Table x Citrix Services” 将表格向右移动三个制表位。

示例文档可以在 https://dl.dropbox.com/u/43555945/StackOverflowSample.docx 找到

这是我的测试代码:

$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
}

最佳答案

使用 $Selection.InsertCaption(-2, "Citrix Services") 通过将该代码放置在构建行的 foreach 循环之上来创建表标题。
第一个参数是标题类型; -2 是表格,其他可通过负整数访问的内置标签是图形和方程。

使用 rows.setleftindent() 将表格缩进到您想要的位置。第一个参数是缩进距离,第二个参数定义标尺样式。 在缩进后移动您的 autofitbehavior 实例以确保表格正确自动调整。

在构建表格行的 foreach 循环之后将以下几行添加到您的代码中。

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

希望这对您有所帮助!

关于PowerShell Word 2007/2010/2013 向表格添加标题并将表格 3 选项卡移至右侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14507815/

相关文章:

Powershell - 在文本文件的开头添加文本字符串

iis - 无法安装 Powershell 管理单元

PHPWord 将一行中的某些单词加粗

vba - 使用 VBA 在 MS-Word 中插入和编辑 Mathtype 方程

json - 从类型 JSON 属性中提取属性

asp.net - 使用 powershell 将 .NET Framework 从 4.5 升级到 4.6

powershell - -替换运算符捕获组为我提供了捕获的子字符串的倒数

c# - STA 中的 Akka.net 参与者

perl - 如何从 Perl 以只读方式打开 Word 文档?

.net - 使用 itextsharp 阅读 PDF,其中 PDF 语言为非英语