I need some help with this code. It works like 99%, but every once in a while I get print from the PRN file that goes beyond the 2 space placeholder that I set in the bottom part of the code. Essentially, my users wanted to simplify their life and complicate mine...sigh. Here's the code:
# ----------------------------------------------------- function Release-Ref ($ref) { ([System.Runtime.InteropServices.Marshal]::ReleaseComObject( [System.__ComObject]$ref) -gt 0) [System.GC]::Collect() [System.GC]::WaitForPendingFinalizers() } # -----------------------------------------------------
$user = $env:USERNAME $csvFile = $args $path = \\domain\common\Users\+$user+\AccubidJonas\Jonas.prn #Test path of file if(Test-Path $path){ Remove-Item $path } $processes = Import-Csv -Path $csvFile $Excel = New-Object -ComObject excel.application $Excel.DisplayAlerts = $false $Excel.visible = $false $Workbook = $Excel.workbooks.add() $WorkSheet = $Workbook.ActiveSheet $Columns = $WorkSheet.Columns $xlDelimited = $true $xlTextQualifierNone = -4142 $xlShiftToRight = -4161 $xlTextPrinter = 36 $xlOtherChar = "|" $Excel.cells.item(1,1) = "Cost" $Excel.cells.item(1,2) = "" $Excel.cells.item(1,3) = "" $Excel.cells.item(1,4) = "LH" $Excel.cells.item(1,5) = "" $Excel.cells.item(1,6) = "L" $Excel.cells.item(1,7) = "" $Excel.cells.item(1,8) = "M" $Excel.cells.item(1,9) = "" $Excel.cells.item(1,10) = "S" $Excel.cells.item(1,11) = "" $Excel.cells.item(1,12) = "E" $Excel.cells.item(1,13) = "" $Excel.cells.item(1,14) = "O" $i = 2 foreach($process in $processes) { $Excel.cells.item($i,1) = $process.Phase $Excel.cells.item($i,4) = $process."Total Hours" $Excel.cells.item($i,6) = $process."Total Labor $" $Excel.cells.item($i,8) = $process."Total Material" $Excel.cells.item($i,10) = $process.Subs $Excel.cells.item($i,12) = $process.Equipment $Excel.cells.item($i,14) = $process."General Expenses" $i++ } end foreach process $Range = $WorkSheet.Range("A1").EntireColumn $Range2 = $WorkSheet.Range("A1") $TextToColumns = $Range.TextToColumns($Range2,$xlDelimited,$xlTextQualifierNone,$true,$false,$false,$false,$false,$true,$xlOtherChar) $WorkSheet.Cells.Item(2.1).Value() = "Desc" $Range = $WorkSheet.Range("B1").EntireColumn [void] $Range.Insert($xlShiftToRight) $Range = $WorkSheet.UsedRange [void] $Range.EntireColumn.Autofit() $Columns.item("B:B").columnwidth=2 $Columns.item("D:D").columnwidth=2 $Columns.item("F:F").columnwidth=2 $Columns.item("H:H").columnwidth=2 $Columns.item("J:J").columnwidth=2 $Columns.item("L:L").columnwidth=2 $Columns.item("N:N").columnwidth=2 $Workbook.saveas($path,$xlTextPrinter) $Excel.Quit() Remove-Variable -Name excel $a = Release-Ref($Workbook) $a = Release-Ref($Columns) $a = Release-Ref($Range) $a = Release-Ref($Range2) $a = Release-Ref($WorkSheet) Stop-Process -processname powershell
I've taken a basic script from Technet and applied various additions with guidance from their convoluted reference library to come up with the code above. This issue is that when I call the
$Range = $WorkSheet.Range("B1").EntireColumn [void] $Range.Insert($xlShiftToRight)
I find that it sometimes places 2 spaces when converting to PRN and sometimes it places 3--even though I have told it to autosize to a 2 width value. The application that this is going back into calls for 2 spaces in between each value in the printed text file in order for the uploading of information to happen. I create a mock XLS workbook with about 5 rows and 15 columns. I'm stuck on this item since sometimes the "B" column adds a 3 space value and then it messes the upload up.
Thoughts?