Trying to create a table in word and would like to speed up the process a bit, without jobs this works fine but it’s just slow. When I run this script everything works fine except when the script gets to
$Table.Cell($x,<column>).Range.Text = <string>
It gives the error
You cannot call a method on a null-valued expression. + CategoryInfo : InvalidOperation: (Cell:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
and when it gets to
$table = $doc.tables.item(1)
It gives the error
You cannot call a method on a null-valued expression. + CategoryInfo : InvalidOperation: (item:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull
I’m guessing it has something to do with
[System.Runtime.InteropServices.Marshal]::GetActiveObject('Word.Application')
not taking all of the document’s information…
Here’s my code
$owners = Import-CSV $ownerspath -header @(“Owners”) foreach($name in $owners) {
#Document creation [ref]$SaveFormat = “microsoft.office.interop.word.WdSaveFormat” -as [type]
$Word = New-Object -comobject word.application $Word.Visible = $false $Doc = $Word.Documents.Add() $Range = $Doc.Range() $owner = $name.Owners $g = import-csv $exportpath$owner.csv -header @("Server name", "Description", "OS", "OS EOL", "SQL", "SQL EOL") $path = "$exportpath$owner.docx" if(($g.count + 1) -eq 1) { $rows = 2 } else { $rows = $g.count + 1 } #Table creation $Selection = $word.selection $Doc.Tables.Add($Range,$Rows,6) | Out-Null $Table = $Doc.Tables.item(1) $Table.Style = "Medium Shading 1 - Accent 1" $Table.Cell(1,1).Range.Text = "Server name" $Table.Cell(1,2).Range.Text = "Server description" $Table.Cell(1,3).Range.Text = "OS Version" $Table.Cell(1,4).Range.Text = "OS EOL Date" $Table.Cell(1,5).Range.Text = "SQL Version" $Table.Cell(1,6).Range.Text = "SQL EOL Date" $x = 2 foreach($l in $g) { $sn = $l.'Server name' $d = $l.Description $oper = $l.OS $opereol = $l.'OS EOL' $sequel = $l.SQL $sequeleol = $l.'SQL EOL' $scriptblock = { param($sn, $d, $oper, $opereol, $sequel, $sequeleol, $x) $word = [System.Runtime.InteropServices.Marshal]::GetActiveObject('Word.Application') $doc = $word.documents $table = $doc.tables.item(1) #Server names, OS, OS EOL, SQL, SQL EOL, and coloring for the EOL dates if the need them are added in this foreach loop. $Table.Cell($x,1).Range.Text = $sn $Table.Cell($x,2).Range.Text = $d $Table.Cell($x,3).Range.Text = $oper if($l.'OS EOL' -eq 'Out of date') { $Table.Cell($x,4).Range.shading.BackgroundPatternColor = 255 $Table.Cell($x,4).Range.Text = $opereol $OoDTotal += 1 } elseif($l.'OS EOL' -like "*!*") { $Table.Cell($x,4).Range.shading.BackgroundPatternColor = 65535 $Table.Cell($x,4).Range.Text = $opereol $CloseTotal += 1 } else { $Table.Cell($x,4).Range.Text = $opereol $UtDTotal += 1 } $Table.Cell($x,5).Range.Text = $sequel if($l.'SQL EOL' -eq 'Out of date') { $Table.Cell($x,6).Range.shading.BackgroundPatternColor = 255 $Table.Cell($x,6).Range.Text = $sequeleol $OoDTotal += 1 } elseif($l.'SQL EOL' -like "*!*") { $Table.Cell($x,6).Range.shading.BackgroundPatternColor = 65535 $Table.Cell($x,6).Range.Text = $sequeleol $CloseTotal += 1 } else { $Table.Cell($x,6).Range.Text = $sequeleol $UtDTotal += 1 } } Start-Job $scriptblock -ArgumentList $sn, $d, $oper, $opereol, $sequel, $sequeleol, $x $x++ } while(get-job -state "Running") { start-sleep -seconds 1 } Get-Job | Receive-Job Get-Job | Remove-Job
submitted by /u/w1gray
[link] [comments]
The post Background jobs and COM Objects appeared first on How to Code .NET.