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