Milan Patel
Milan Patel

Reputation: 43

Powershell Adding Multiple Stylized Tables in Word Document Mixed With Text

Essentially I am making a list of invoices as a bill, it will look like this (in word through powershell, note that the word file doesn't pre-emptively exists, i create a new one and start adding to it): enter image description here

The problem is that I only know how to do the following:

$word = New-Object -comobject word.application
$word.Visible = $false
$doc = $word.Documents.Add()
$Selection = $word.Selection
$Selection.Style="Title"
$Selection.Font.Bold = 1
$Selection.ParagraphFormat.Alignment = 2
$Selection.TypeText("Expected Billing")
$Selection.TypeParagraph()
$Selection.Style="No Spacing"
$Selection.Font.Bold = 0
$Selection.TypeParagraph()
$doc.SaveAs([ref]$savepath) 
$doc.Close() 
$word.quit()

Adding text is easy but when it comes to adding tables, I cant get it right. As you can see:

Actually the numbered tables aren't supposed to have borders either but I left them so you visually see. Then of course the total calculated amount which I can do. But notice it comes after the tables so I want tables appending and want to know how to append after them. Can someone help me out? I don't know the coding for this.

Upvotes: 0

Views: 1586

Answers (1)

Milan Patel
Milan Patel

Reputation: 43

Okay after doing more research I figured it out. I will post the code

    $Range = $Selection.Range
    $Table = $Selection.Tables.add($Selection.Range,2,5)
    $Table.cell(1,1).range.text = "Item Code"
    $Table.cell(2,1).range.text = "Description"

    $Table.cell(1,2).range.text = "Quantity"
    $Table.cell(1,3).range.text = "Amount"
    $Table.cell(1,4).range.text = "SUP"
    $Table.cell(1,5).range.text = "Dealer Code"
    $Selection.EndKey(6) | Out-Null

You have to start with the add and make sure you end with End Key(6)

To edit width or font style:

   $Table.cell(1,1).Width = 222.2 
   $Table.cell(1,1).range.Font.bold = 1 

Upvotes: 0

Related Questions