New-Object -ComObject Word.Application

Product: PowerShell Universal
Version: 1.5.19

Hi @adam I am writing a dashboard for WooCommerce, when an order comes in i need to print to a POS printer. This is installed as a printer on the dashboard server (printer1). What i want to do is advanced printing and make use of ComObject Word.Application so i can modify the settings, and print to specific printers but i cant get it to work. This works fine from pwsh on the PSU node, logged in as the same account as the dashboard service runs from (domain\administrator). Word is also installed and activated against the same user on the same node…

It doesnt error at all, no logs but it fails straight away on ‘New-Object -ComObject Word.Application’

I have added toasts to see the output, and there is nothing after New-Object. I have also tried this from Start-Process with credentials but again nothing happens.

Any thoughts?

New-UDButton -Text 'Print' -OnClick {
        ### Print Setup
        $Path = "C:\ProgramData\UniversalAutomation\Repository\Data\123.txt"
        Show-UdToast -Message "$($Path)" -Duration 10000
        $WordObj = New-Object -ComObject Word.Application
        Show-UdToast -Message "$($WordObj)" -Duration 10000
        $WordObj.Documents.Add($Path) > $null
        $WordObj.ActiveDocument.Content.Font.Size = 12
        $WordObj.ActiveDocument.Content.Font.Name = "Verdana"
        $WordObj.ActiveDocument.Sections.PageSetup.LeftMargin = 200
        $WordObj.ActiveDocument.Sections.PageSetup.TopMargin = 0
        $WordObj.ActivePrinter = "printer1"
        #Send To Default Printer
        $WordObj.PrintOut()
        # Close Connection to printer
        $WordObj.ActiveDocument.Close(0)
        $WordObj.quit() 
    }

Does the dashboard log show any errors? Or it just doesn’t do anything?

No errors at all, and you see some toasts in there, the first one fires, the second does not, so it appears to have trouble creating the Word.Application ComObject.

How are you hosting PSU?

Correct, PSU, 1.5.19 via MSI install, running the service as domain\administrator

Oh now i get a error, i wasnt getting any yesterday…

[06-07-21 04:56:05 PM] An error occurred: Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070520 A specified logon session does not exist. It may already have been terminated. (0x80070520).
Endpoint: d66678bd-13a3-4398-be68-ba03caec171b
Session: 47c573a0-b03a-4112-bc1a-f4cd32738b56
File: C:\ProgramData\UniversalAutomation\Repository\Data\Pages\Staging\TestCode.ps1
Endpoint Start Line: 37
Endpoint End Line: 54
Stack Trace: at , : line 4

I’ve found this error mentioned in a few places but this one seems the most applicable. Can you try setting this policy to see if it changes the behavior? Task Scheduler Error “A specified logon session does not exist” - Microsoft Tech Community

That setting was already correct and it doesnt work… To be fair, i have done a lot of testing with this today and it just wont work… comobject for word wont work through invoke-command, enter-pssession.

have tried using :
$PrintDocument = New-Object System.Drawing.Printing.PrintDocument

but I have got frustrated with it and given up for good old:
Start-Process $Path -Verb PrintTo('Printer1')

The formatting is horrible on the ticket, and some serious messing with settings it kinda works, but not ideal… If anyone knows how to get either PrintDocument or Word.Application working then please reply!

Thanks Adam for looking!

Sussed it…

function PrintReceipt($InputDocument, $Printer)
{
    $PrintPageHandler =
    {
        param([object]$sender, [System.Drawing.Printing.PrintPageEventArgs]$ev)
        $linesPerPage = 0
        $yPos = 0
        $count = 0
        $leftMargin = 75
        $topMargin = -25
        $line = $null
        $printFont = New-Object System.Drawing.Font "Verdana", 12
        # Calculate the number of lines per page.
        $linesPerPage = $ev.MarginBounds.Height / $printFont.GetHeight($ev.Graphics)
        # Print each line of the file.
        while ($count -lt $linesPerPage -and (($line = $streamToPrint.ReadLine()) -ne $null))
        {
            $yPos = $topMargin + ($count * $printFont.GetHeight($ev.Graphics))
            $ev.Graphics.DrawString($line, $printFont, [System.Drawing.Brushes]::Black, $leftMargin, $yPos, (New-Object System.Drawing.StringFormat))
            $count++
        }
    }
    Add-Type -AssemblyName System.Drawing
    $doc = New-Object System.Drawing.Printing.PrintDocument
    #Show-UdToast -Message "$($doc.name)" -Duration 10000
    $doc.DocumentName = $InputDocument
    $doc.PrinterSettings = New-Object System.Drawing.Printing.PrinterSettings
    $doc.PrinterSettings.PrinterName = $Printer
    $streamToPrint = New-Object System.IO.StreamReader $InputDocument
    $doc.add_PrintPage($PrintPageHandler)
    $doc.PrinterSettings.PrintFileName = "$InputDocument"
    $doc.Print()
    $streamToPrint.Close()
    $doc = $null
}
1 Like