(view source code of printword.ps as plain text)
param (
[parameter( Mandatory = $False, ValueFromPipeline = $True )]
[string] $wordfile,
[string] $printer,
[switch] $h,
[parameter( ValueFromRemainingArguments = $true )]
[object] $invalidArgs
)
if ( $h -or $invalidArgs `
-or $wordfile.Contains( "?" ) `
-or $printer.Contains( "?" ) `
-or ![System.IO.File]::Exists( $wordfile ) `
-or ( ![string]::IsNullOrWhiteSpace( $printer ) -and ( ( Get-WmiObject -Class Win32_Printer -Filter "Name='$printer'" | Measure-Object ).Count -ne 1 ) ) ) {
if ( ![string]::IsNullOrWhiteSpace( $wordfile ) -and !$wordfile.Contains( "?" ) -and ![System.IO.File]::Exists( $wordfile ) ) {
Write-Host
Write-Host "ERROR: " -ForegroundColor Red -NoNewline
Write-Host ( "Word document `"{0}`" not found" -f $wordfile )
} elseif ( ![string]::IsNullOrWhiteSpace( $printer ) -and ( ( Get-WmiObject -Class Win32_Printer -Filter "Name='$printer'" | Measure-Object ).Count -ne 1 ) ) {
Write-Host
Write-Host "ERROR: " -ForegroundColor Red -NoNewline
Write-Host ( "Unknown printer `"{0}`"" -f $printer )
} elseif ( $h -or [bool] $invalidArgs ) {
Write-Host
Write-Host "ERROR: " -ForegroundColor Red -NoNewline
Write-Host "Too many argument(s)"
}
Write-Host
Write-Host "PrintWord.ps1, Version 1.00"
Write-Host "Print a Word document"
Write-Host
Write-Host "Usage: " -NoNewline
Write-Host "powershell.exe . PrintWord.ps1 wordfile [ printer ]" -ForegroundColor White
Write-Host
Write-Host "Where: " -NoNewline
Write-Host "wordfile " -ForegroundColor White -NoNewline
Write-Host "is the Word document to be printed"
Write-Host " printer " -ForegroundColor White -NoNewline
Write-Host "is the optional printer name (default: default printer)"
Write-Host
Write-Host "Note: This script requires Word."
Write-Host " Return code (`"errorlevel`") 1 in case of errors, otherwise 0."
Write-Host
Write-Host "Written by Rob van der Woude"
Write-Host "https://www.robvanderwoude.com"
Exit 1
}
$objWord = New-Object -ComObject Word.Application
# Change printer if specified
if ( $printer ) {
$defaultprinter = $objWord.ActivePrinter
$objWord.ActivePrinter = $printer
}
# Open, print and close the specified Word document
[void] $objWord.Documents.Open( $wordfile, $false, $true )
$objWord.ActiveDocument.PrintOut( )
$objWord.ActiveDocument.Close( $false )
# Restore default printer
if ( $printer ) {
$objWord.ActivePrinter = $defaultprinter
}
$objWord.Quit( )
page last modified: 2024-04-16; loaded in 0.0092 seconds