Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for printword.ps

(view source code of printword.ps as plain text)

  1. param (
  2. 	[parameter( Mandatory = $False, ValueFromPipeline = $True )]
  3. 	[string] $wordfile,
  4. 	[string] $printer,
  5. 	[switch] $h,
  6. 	[parameter( ValueFromRemainingArguments = $true )]
  7. 	[object] $invalidArgs
  8. )
  9.  
  10. if ( $h -or $invalidArgs `
  11.         -or $wordfile.Contains( "?" ) `
  12. 		-or $printer.Contains( "?" ) `
  13.         -or ![System.IO.File]::Exists( $wordfile ) `
  14. 		-or ( ![string]::IsNullOrWhiteSpace( $printer ) -and ( ( Get-WmiObject -Class Win32_Printer -Filter "Name='$printer'" | Measure-Object ).Count -ne 1 ) ) ) {
  15. 	if ( ![string]::IsNullOrWhiteSpace( $wordfile ) -and !$wordfile.Contains( "?" ) -and ![System.IO.File]::Exists( $wordfile ) ) {
  16. 		Write-Host
  17. 		Write-Host "ERROR:   " -ForegroundColor Red -NoNewline
  18. 		Write-Host ( "Word document `"{0}`" not found" -f $wordfile )
  19. 	} elseif ( ![string]::IsNullOrWhiteSpace( $printer ) -and ( ( Get-WmiObject -Class Win32_Printer -Filter "Name='$printer'" | Measure-Object ).Count -ne 1 ) ) {
  20. 		Write-Host
  21. 		Write-Host "ERROR:   " -ForegroundColor Red -NoNewline
  22. 		Write-Host ( "Unknown printer `"{0}`"" -f $printer )
  23. 	} elseif ( $h -or [bool] $invalidArgs ) {
  24. 		Write-Host
  25. 		Write-Host "ERROR:   " -ForegroundColor Red -NoNewline
  26. 		Write-Host "Too many argument(s)"
  27. 	}
  28. 	Write-Host
  29. 	Write-Host "PrintWord.ps1,  Version 1.00"
  30. 	Write-Host "Print a Word document"
  31. 	Write-Host
  32. 	Write-Host "Usage:   " -NoNewline
  33. 	Write-Host "powershell.exe . PrintWord.ps1  wordfile  [ printer ]" -ForegroundColor White
  34. 	Write-Host
  35. 	Write-Host "Where:   " -NoNewline
  36. 	Write-Host "wordfile   " -ForegroundColor White -NoNewline
  37. 	Write-Host "is the Word document to be printed"
  38. 	Write-Host "         printer    " -ForegroundColor White -NoNewline
  39. 	Write-Host "is the optional printer name (default: default printer)"
  40. 	Write-Host
  41. 	Write-Host "Note:    This script requires Word."
  42. 	Write-Host "         Return code (`"errorlevel`") 1 in case of errors, otherwise 0."
  43. 	Write-Host
  44. 	Write-Host "Written by Rob van der Woude"
  45. 	Write-Host "https://www.robvanderwoude.com"
  46.  
  47. 	Exit 1
  48. }
  49.  
  50. $objWord = New-Object -ComObject Word.Application
  51.  
  52. # Change printer if specified
  53. if ( $printer ) {
  54. 	$defaultprinter = $objWord.ActivePrinter
  55. 	$objWord.ActivePrinter = $printer
  56. }
  57.  
  58. # Open, print and close the specified Word document
  59. [void] $objWord.Documents.Open( $wordfile, $false, $true )
  60. $objWord.ActiveDocument.PrintOut( )
  61. $objWord.ActiveDocument.Close( $false )
  62.  
  63. # Restore default printer
  64. if ( $printer ) {
  65. 	$objWord.ActivePrinter = $defaultprinter
  66. }
  67.  
  68. $objWord.Quit( )
  69.  

page last modified: 2024-04-16; loaded in 0.0314 seconds