Taher Shaker
Taher Shaker

Reputation: 25

Adjusting a Word Table Column Width using Powershell

I am writing a script that will gather some data and do some checks on that data. If a check fails then I will add it to an ArrayList. Later I would need to add this into a table in the Word document. The script works fine, however the first column is too big, for example the table would be like this:

enter image description here

What i need to do is to shrink the width of the first column to be like this:

enter image description here

I have tried to use the .SetWidth and the .Width but both did not work.

My script is as follows:

#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++
    }

Upvotes: 0

Views: 1693

Answers (2)

Romuald bezard
Romuald bezard

Reputation: 1

According Microsoft's documentation, .Width can only be used to set width in points:

Columns.Width property (Word)

Returns or sets the width of the specified columns, in points. Read/write Long.

So there's no need to use the .PreferredWidthType setting.

If you want to use the type 2 (wdPreferredWidthPercent seems to be better in your case), you should use something like:

$Table.Columns(1).PreferredWidthType = 2
$Table.Columns(1).PreferredWidth = 25

Upvotes: 0

macropod
macropod

Reputation: 13505

You need to tell Word what kind of width setting you're using, plus how many units of that you want to use. Try:

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

Note: PreferredWidthType 3 is for wdPreferredWidthPoints. 1 point = 1/72in. As coded, 144 points is thus 2in. You might prefer to use PreferredWidthType 2, which is for wdPreferredWidthPercent, combined with an appropriate number for the percent. You may also want to apply the PreferredWidthType and Width to the entire column, not just the cell being worked on.

Upvotes: 1

Related Questions