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 Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,598 questions
{count} votes

Accepted answer
  1. SethWH 436 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. Ian Xue 38,846 Reputation points Microsoft Vendor
    2020-10-20T07:11:36.787+00:00

    Hi,
    Please see if this works

    $wd.PrintOut([ref]$BackGround, [ref]$Append, [ref]$Range, [ref]$Missing,[ref]$Missing, [ref]$Missing,[ref]$Item,[ref]$Copies,[ref]$Pages)  
    

    Best Regards,
    Ian

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

    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.

    1 person found this answer helpful.

  2. Rich Matheisen 47,496 Reputation points
    2020-10-19T18:03:00.157+00:00

    Working with COM objects stinks, doesn't it?

    What happens if you use "$Pages = [object]"2-3"" instead of what you have now?

    The reason for the 2nd exception is that you're using a negative number. When you assigned the value "2-3" (without the quotes!) to $Pages you subtracted 3 from 2.

    0 comments No comments

  3. Christian Bahnsen -- .mil account 201 Reputation points
    2020-10-19T18:43:53.047+00:00

    @Rich Matheisen

    You're right about working with COM objects. Thanks for the suggestion, but then I got this error:

    You cannot call a method on a null-valued expression.
    At line:1 char:2

    • $doc.PrintOut([ref]$BackGround, [ref]$Append, [ref]$Range, [ref]$Mis ...
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidOperation: (:) [], RuntimeException
    • FullyQualifiedErrorId : InvokeMethodOnNull
    0 comments No comments

  4. SethWH 436 Reputation points
    2020-10-19T20:45:24.76+00:00

    This is interesting because I remember using printout() in older versions of PowerShell. So... typically you only have to fill in the missing values up until your last needed variable like $pages (as your script does correctly). One thing I noticed, if you add the next ref variable PageType as [ref]$Missing, it doesn't seem to care about the previous [ref]$Pages error. It says something along the lines can't convert 'Missing' to type object, etc. So basically, it's always complaining about the last ref variable. I wonder if this is a bug. I may run some tests later on older versions.


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.