Powershell printing Word 2007

Filip 831 Reputation points
2021-03-10T12:52:04.28+00:00

Hello
I tried print docx in powershell to printer but when i do it all time it show an error: "Cannot convert the "2-4" value of type "string" to type "Object".""
I tried find and make many sulotion but all time it show only this.
Can be the reason why it doesn't work that i have Word 2007?
Thanks for answare

76247-ee.png

$w = New-Object -ComObject Word.Application

$w.visible = $true

$doc = $w.Documents.Open('c:\text.docx')

$Missing = [System.Reflection.Missing]::Value # Default values
$BackGround = 0
$Append = 0
$Range = 4
$OutputFileName = $Missing
$From = $Missing
$To = $Missing
$Item = 0
$Copies = 1 # Number of copies
$Pages = "2-4" # Page range

$w.printout([ref]$BackGround, [ref]$Append, [ref]$Range, [ref]$OutputFileName, [ref]$From, [ref]$To, [ref]$Item, [ref]$Copies, [ref]$Pages)

$doc.close($false)

$w.Quit()

Windows Server Printing
Windows Server Printing
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.Printing: Printer centralized deployment and management, scan and fax resources management, and document services
675 questions
{count} votes

Accepted answer
  1. Ian Xue 36,751 Reputation points Microsoft Vendor
    2021-03-18T06:27:58.883+00:00

    Hi,

    This script works fine for me.

    $word = New-Object -ComObject Word.Application  
    $doc = $word.Documents.Open('c:\text.docx')  
    $Missing = [System.Reflection.Missing]::Value   
    $BackGround = 0  
    $Append = 0  
    $Range = 4  
    $OutputFileName = $Missing  
    $From = $Missing  
    $To = $Missing  
    $Item = 0  
    $Copies = 1   
    $Pages = "2-4"  
    $word.printout([ref]$BackGround, [ref]$Append, [ref]$Range, [ref]$OutputFileName, [ref]$From, [ref]$To, [ref]$Item, [ref]$Copies, [ref]$Pages)   
    $doc.close($false)     
    $word.Quit()  
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($doc)  
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($word)  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.