使用Powershell调整Word表格列宽

时间:2018-04-04 16:09:45

标签: powershell ms-word

我正在编写一个脚本,它将收集一些数据并对该数据进行一些检查。如果检查失败,那么我将它添加到ArrayList。稍后我需要将它添加到Word文档的表中。该脚本工作正常,但第一列太大,例如表格将如下:

enter image description here

我需要做的是缩小第一列的宽度:

enter image description here

我曾尝试使用.SetWidth和.Width,但两者都不起作用。

我的脚本如下:

#Create and load the MW-Word Application.
$Word = New-Object -Com Word.Application
#Make it visable after opening the application.
$Word.Visible = $true
#Create a new document.
$Doc = $Word.Documents.Add();
$Selection = $Word.Selection
#Create the header of the MS-Word Document with the required Data
$Doc.ActiveWindow.ActivePane.View.SeekView = 1
#Call and add a PIC that is already created and saved in this tool folder path.
$HeaderPic = $Selection.InlineShapes.AddPicture($scriptPath + "\Word\Word-Doc-Header.png")
$HeaderPic.Height = 100
$HeaderPic.width = 450
#Create the Footer of the MS-Word Document with the required Data
$Doc.ActiveWindow.ActivePane.View.SeekView = 4
$Selection.Font.Size = 12
$Selection.Font.Bold = $true
$Selection.TypeText("VMware Inc.")
$Selection.TypeParagraph()
$Selection.TypeText("$CustomerName.")
$Doc.ActiveWindow.ActivePane.View.SeekView = 0
$Selection.TypeParagraph()
$Selection.TypeParagraph()
$Selection.TypeParagraph()
$Selection.TypeParagraph()
$Selection.TypeParagraph()
    $Selection.Style = "Heading 1"
    $Selection.Font.Underline = 1
    $Selection.TypeText("Check List Results:")
    $Selection.TypeParagraph()
    $Selection.TypeParagraph()
    #Print the message, introduction, warnings and HyperLinks (if required) for this subsection.
    $Selection.Style = "Normal"
    $Selection.Font.Color = "6316128"
    $Selection.TypeText("The below tables will provide all the detected issues within the performed check list according to the component and severity.")
    $Selection.TypeParagraph()
    $Selection.TypeParagraph()
    $Selection.Style = "Heading 1"
    $Selection.Font.Underline = 1
    $Selection.TypeText("NSX Manager P1 Checks:")
    $Selection.TypeParagraph()
    $Selection.TypeParagraph()

    $TableRange = $Selection.Range
    $NsxManagerP1CheckTable = $Doc.Tables.add($TableRange,2,2)


    $NsxManagerP1CheckTable.cell(1,1).range.text = "Priority"
    $NsxManagerP1CheckTable.cell(1,1).Width = 1
    $NsxManagerP1CheckTable.cell(1,1).Range.Font.Bold = $true
    $NsxManagerP1CheckTable.cell(1,2).range.text = "Issue Detected"
    $NsxManagerP1CheckTable.cell(1,2).Range.Font.Bold = $true
    $ReqCellWidth = $NsxManagerP1CheckTable.cell(1,1).Width

    $Row = 2
    Foreach ($Issue in $NsxHostClusterP1CheckList) {
        $NsxManagerP1CheckTable.cell($Row,1).range.text = "P1"
        $NsxManagerP1CheckTable.cell($Row,1).Width = 1
        $NsxManagerP1CheckTable.cell($Row,1).Range.Font.Bold = $true
        $NsxManagerP1CheckTable.cell($Row,2).range.text = $Issue 
        $NsxManagerP1CheckTable.Rows.Add()
        $Row++
    }

1 个答案:

答案 0 :(得分:0)

你需要告诉Word你正在使用什么样的宽度设置,以及你想要使用多少单位。尝试:

$NsxManagerP1CheckTable.cell($Row,1).PreferredWidthType = 3
$NsxManagerP1CheckTable.cell($Row,1).Width = 144

注意: PreferredWidthType 3适用于wdPreferredWidthPoints。 1分= 1 / 72in。如编码,144点因此是2in。您可能更喜欢使用PreferredWidthType 2(适用于wdPreferredWidthPercent),并使用适当的百分比数字。您可能还希望将PreferredWidthType和Width应用于整个列,而不仅仅是正在处理的单元格。

相关问题