Scroll Top
Evotec Services sp. z o.o., ul. Drozdów 6, Mikołów, 43-190, Poland

PSWriteWord – Updated to 0.4.1 (Breaking Change included)

PSWriteWord 0.4.1

While the update may seem small – change from 0.4 to 0.4.1 it actually brought a number of fixes and new features. One of the early adopters found some issues with how Add-WordTable worked so I've spent a lot of time fixing different cases on how people may use it. During that time I've created a bunch of different examples covering possible use cases. While with PSWriteWord you can build your table by hand… it has quite nice functionality that I think is very useful in your day to day scenario. To add few more features I had to change old -Table to -DataTable and use -Table parameter for something else. So if you've managed to use the old version… you may need to change just one bit.

PSWriteWord Information
Please notice this article contains parts of information (still useful) and may not reflect all functionalities of this module. For download, source code and so on you should refer to the dedicated PSWriteWord module page. After reading this one… of course! It contains useful informationexamples and know-how.

Let's take a look at few examples… shall we?

How can I use it?

Below example is simple use case. Trying to build an invoice table with 2 columns Description and Amount. Normally you would have to go thru adding rows and columns looping thru, adding alignments, design and so on.  While you can still do that… you don't have to.

Import-Module PSWriteWord #-Force

$FilePath = "$Env:USERPROFILE\Desktop\PSWriteWord-Example-Tables3.docx"

#Clear-Host
$WordDocument = New-WordDocument $FilePath

$InvoiceEntry1 = @{}
$InvoiceEntry1.Description = 'IT Services 1'
$InvoiceEntry1.Amount = '$200'

$InvoiceEntry2 = @{}
$InvoiceEntry2.Description = 'IT Services 2'
$InvoiceEntry2.Amount = '$300'

$InvoiceEntry3 = @{}
$InvoiceEntry3.Description = 'IT Services 3'
$InvoiceEntry3.Amount = '$288'

$InvoiceEntry4 = @{}
$InvoiceEntry4.Description = 'IT Services 4'
$InvoiceEntry4.Amount = '$301'

$InvoiceEntry5 = @{}
$InvoiceEntry5.Description = 'IT Services 5'
$InvoiceEntry5.Amount = '$299'

$InvoiceData = @()
$InvoiceData += $InvoiceEntry1
$InvoiceData += $InvoiceEntry2
$InvoiceData += $InvoiceEntry3
$InvoiceData += $InvoiceEntry4
$InvoiceData += $InvoiceEntry5

Add-WordText -WordDocument $WordDocument -Text "Invoice Data" -FontSize 15 -Alignment center
Add-WordParagraph -WordDocument $WordDocument
Add-WordTable -WordDocument $WordDocument -DataTable $InvoiceData -Design LightShading

Save-WordDocument $WordDocument

### Start Word with file
Invoke-Item $FilePath

Got anything more?

Another example shows couple of different Add-WordTable examples. It covers 3 cases. Hash, Ordered Hash and Array of Sytem.Objects. All 3 are supported by Add-WordTable. Notice how you need to have [ordered] for hash to get proper order of values.  Also notice how column names are given by parameter (or by default Name/Value).

### prepare data
$hash = [ordered] @{}
$hash.add("HQ-1", "5.54.546")
$hash.add("EUR-1", "6.0.0.1")
$hash.add("HQ-2", "5.6")
$hash.add("EUR-2", "6.1.5")
$hash.add("EUR-3", "6.2")

$hash1 = @{}
$hash1.add("HQ-1", "5.54.546")
$hash1.add("EUR-1", "6.0.0.1")
$hash1.add("HQ-2", "5.6")
$hash1.add("EUR-2", "6.1.5")
$hash1.add("EUR-3", "6.2")

$hash2 = @{}
$hash2.add("HQ-1", "5.54.546")

$obj = New-Object System.Object
$obj | Add-Member -type NoteProperty -name Name -Value "Ryan_PC"
$obj | Add-Member -type NoteProperty -name Manufacturer -Value "Dell"
$obj | Add-Member -type NoteProperty -name ProcessorSpeed -Value "3 Ghz"
$obj | Add-Member -type NoteProperty -name Memory -Value "6 GB"

$myObject2 = New-Object System.Object
$myObject2 | Add-Member -type NoteProperty -name Name -Value "Doug_PC"
$myObject2 | Add-Member -type NoteProperty -name Manufacturer -Value "HP"
$myObject2 | Add-Member -type NoteProperty -name ProcessorSpeed -Value "2.6 Ghz"
$myObject2 | Add-Member -type NoteProperty -name Memory -Value "4 GB"

$myObject3 = New-Object System.Object
$myObject3 | Add-Member -type NoteProperty -name Name -Value "Julie_PC"
$myObject3 | Add-Member -type NoteProperty -name Manufacturer -Value "Compaq"
$myObject3 | Add-Member -type NoteProperty -name ProcessorSpeed -Value "2.0 Ghz"
$myObject3 | Add-Member -type NoteProperty -name Memory -Value "2.5 GB"

$myArray = @($obj, $myobject2, $myObject3)
### prepare data end

Import-Module PSWriteWord #-Force
$FilePath = "$Env:USERPROFILE\Desktop\PSWriteWord-Example-Tables6.docx"

$WordDocument = New-WordDocument $FilePath
Add-WordTable -WordDocument $WordDocument -DataTable $hash -Design ColorfulList #-Verbose
Add-WordParagraph -WordDocument $WordDocument

Add-WordTable -WordDocument $WordDocument -DataTable $hash -Design ColorfulGrid -Columns 'My Name', 'My Value'
Add-WordParagraph -WordDocument $WordDocument

Add-WordTable -WordDocument $WordDocument -DataTable $hash1 -Design ColorfulGrid -Columns 'My Name', 'My Value' -AutoFit Window
Add-WordParagraph -WordDocument $WordDocument

Add-WordTable -WordDocument $WordDocument -DataTable $hash2 -Design ColorfulGrid -Columns 'My Name', 'My Value'
Add-WordParagraph -WordDocument $WordDocument

Add-WordTable -WordDocument $WordDocument -DataTable $myArray -Design ColorfulList #-Verbose

Save-WordDocument $WordDocument -Language 'en-US'
Invoke-Item $FilePath

PSWriteWord-Tables-Example6

PSWriteWord Information
Please notice this article contains parts of information (still useful) and may not reflect all functionalities of this module. For download, source code and so on you should refer to the dedicated PSWriteWord module page. After reading this one… of course! It contains useful informationexamples and know-how.

There are far more changes that has been added with this release but since it's a fast update… well you will have to find them out by yourself for now!

Related Posts