question

Filip-2434 avatar image
0 Votes"
Filip-2434 asked Filip-2434 commented

Powershell printing Word 2007

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-print
ee.png (37.3 KiB)
· 5
5 |1600 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.

Hi @Filip-2434 ,
What is the variable $doc? Can you post the script?

0 Votes 0 ·

Hello IanXue-MSFT
For example i used this script. But i tried also many other variant of scripts and all time it show this error.
Thanks for answare.

0 Votes 0 ·

Also i tried this:
But it show my other error

$BackGround = 1
$Append = 0
$Range = 4
$Item = 0
$Copies = 1
$Pages = 1-2

$Missing = [System.Reflection.Missing]::Value
$wd = New-Object -ComObject Word.Application

$wd.Documents.Open("C:\test.docx")


$doc = $wd.Documents.Open("C:\test.docx")
$doc.printout([ref]$BackGround, [ref]$Append, [ref]$Range, [ref]$Missing,[ref]$Missing,[ref]$Missing,[ref]$Item,[ref]$Copies,[ref]$Pages)
$doc.Quit()


77463-a.png


0 Votes 0 ·
a.png (731.9 KiB)

Hi Filip,
I have Microsoft Office 2016 installed on my computer and the Application.PrintOut method works well for me, so the issue appears more related to office. You can start a new thread with the tag "office-vba-dev".

0 Votes 0 ·
Show more comments

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered Filip-2434 commented

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.

· 3
5 |1600 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.

Hello @IanXue-MSFT
I have still the same error.(
Thanks for help.

0 Votes 0 ·

You may start a thread with the office-vba-dev tag for better help.

0 Votes 0 ·

Ok i will try thanks).

0 Votes 0 ·