question

ChristianBahnsenmilaccount-1323 avatar image
0 Votes"
ChristianBahnsenmilaccount-1323 asked ·

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

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://docs.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
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

As issues with "office-itpro" tag focus on general questions of Office clients, I would remove this tag. Thanks for your understanding.

0 Votes 0 ·
SethWH avatar image
0 Votes"
SethWH answered ·

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)
· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered ·

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.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ChristianBahnsenmilaccount-1323 avatar image
0 Votes"
ChristianBahnsenmilaccount-1323 answered ·

@RichMatheisen-8856

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

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SethWH avatar image
0 Votes"
SethWH answered ·

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.

· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I spent about 30 minutes on this and found the same thing! No matter which parameter is the last one, it's that one that evokes the error!

0 Votes 0 ·
IanXue-MSFT avatar image
1 Vote"
IanXue-MSFT answered ·

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 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks, Ian. Using PrintOut as an Application method (as opposed to a Document method) has eliminated the error messages.

I think I have a printer driver issue over and above the problem with the code itself, because I'm still not getting hard copy from our printer. I've enlisted our IT guys to help address that issue.

0 Votes 0 ·
ChristianBahnsenmilaccount-1323 avatar image
0 Votes"
ChristianBahnsenmilaccount-1323 answered ·

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 @IanXue-MSFT @RichMatheisen-8856

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ChristianBahnsenmilaccount-1323 avatar image
0 Votes"
ChristianBahnsenmilaccount-1323 answered ·

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
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Filip-2434 avatar image
0 Votes"
Filip-2434 answered ·

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



·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.