powershell -- printing from Word 2016 using the .PrintOut() method

Christian Bahnsen -- .mil account 201 Reputation points
2020-10-19T15:35:56.253+00:00

I found an article at https://social.technet.microsoft.com/Forums/windowsserver/en-US/0aa5d8ab-e756-4a19-96e4-03384a4b32c3/print-a-range-of-pages that looks to do almost exactly what I want to do, but I'm getting an error when I try to use the code.

# create a word object  
$wd = New-Object -ComObject Word.Application  

# make it visible  
$wd.visible = $true  

# create a document object by opening a file  
$doc = $wd.Documents.Open('\\servername\Users\Christian.Bahnsen\Desktop\ToDo Folder\word\splitTest\baseDocument.docx')  

#Print required Page  
$Missing    = [System.Reflection.Missing]::Value  # use default parameter values  
$BackGround = 1  
$Append     = 0  
$range = 4 # see https://learn.microsoft.com/en-us/office/vba/api/word.wdprintoutrange for enumerations  
$Item = 0  
$Copies = 1  
$Pages = "2-3"  

# let's print the range  
$doc.PrintOut([ref]$BackGround, [ref]$Append, [ref]$Range, [ref]$Missing,[ref]$Missing, [ref]$Missing,[ref]$Item,[ref]$Copies,[ref]$Pages)  

I get the following error:

Exception setting "PrintOut": Cannot convert the "2-3" value of type "string" to type "Object".
At line:1 char:1

  • $doc.printout([ref]$BackGround, [ref]$Append, [ref]$Range, [ref]$Miss ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : NotSpecified: (:) [], MethodException
  • FullyQualifiedErrorId : RuntimeException

I changed $Pages to:
$Pages = 2-3
and the error message changes to

Exception setting "PrintOut": Cannot convert the "-1" value of type "int" to type "Object".
At line:2 char:1

  • $doc.printout([ref]$BackGround, [ref]$Append, [ref]$Range, [ref]$Miss ...
  • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : NotSpecified: (:) [], MethodException
  • FullyQualifiedErrorId : RuntimeException

My goal is to print a single page, but at this point I need to figure out why this code is throwing errors. This code is using enumeration 4 (wdPrintRangeOfPages) for the WdPrintOutRange. I've tried using 3 (wdPrintFromTo) but it throws errors, too. Or if I could figure out how to select a single page I might be able to use 2 (wdPrintCurrentPage) or 0 (wdPrintSelection) and wouldn't need to define a range.

Thanks in advance for any assistance.

Christian Bahnsen

Windows for business | Windows Server | User experience | PowerShell
{count} votes

Answer accepted by question author
  1. SethWH 441 Reputation points
    2020-10-20T21:38:36.757+00:00

    I was able to print pages 2-3 using this code. Also, I had some orphaned winword processes that I needed to kill with the "Get-Process WINWORD | stop-Process -force" command. Here it is:

    # Open Word Document 
    [Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Interop.Word") | Out-Null
    $FileName = "C:\Test\TestDoc.docx" # Doc with 3 pages
    $Word = New-Object -comobject Word.Application
    $Word.Visible  = $true # Use $false to not show document
    $word.Documents.Open($FileName)
    
    #Print ref
    $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-3"   # Page range
    
    $Word.printout([ref]$BackGround, [ref]$Append, [ref]$Range, [ref]$OutputFileName, [ref]$From, [ref]$To, [ref]$Item, [ref]$Copies, [ref]$Pages)
    
    # Close Word
    $Word.Quit()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Word)
    

7 additional answers

Sort by: Most helpful
  1. Christian Bahnsen -- .mil account 201 Reputation points
    2020-10-21T14:34:24.107+00:00

    Thanks SethWH and IanXue.

    I'm no longer getting error messages but I think I have a printer driver issue that's keeping me from getting hardcopy. As soon as the printer issue is resolved and I can confirm your code is working I'll flag this as an answer.

    @SethWH @Anonymous @Rich Matheisen

    0 comments No comments

  2. Christian Bahnsen -- .mil account 201 Reputation points
    2020-10-22T12:30:26.787+00:00

    Thanks to everyone for their help! Here's the code I wound up using:

      # create a word object
     $w = New-Object -ComObject Word.Application
    
     # make it visible
     $w.visible = $true
    
     # create a document object by opening a file
     $doc = $w.Documents.Open('\\servername\Users\Christian.Bahnsen\Desktop\ToDo Folder\word\splitTest\5pageDocument.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)
    
     # close the document without saving changes
    $doc.close($false)
    
    $w.Quit()
    
    # housekeeping to free up system resources
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($w)
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($doc)
    [System.GC]::Collect()
    [System.GC]::WaitForPendingFinalizers()
    
    # make sure there are no instances of word still running
    get-process winword 
    
    # more housekeeping
    Remove-Variable w, doc, Missing, BackGround, Append, Range, OutputFileName, From, To, Item, Copies, Pages
    
    0 comments No comments

  3. Filip 831 Reputation points
    2021-03-09T11:22:08.24+00:00

    Hello
    I tried use this sulotion but it doesn't work. Always it show: "Cannot convert the "2-4" value of type "string" to type "Object".""
    Can be the reason why it doesn't work that i have Word 2007?
    Thanks for answare

    0 comments No comments

Your answer

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