This example will show you how to create a simple VBScript that will control the PDF Writer. The script creates a runonce file and launches a print job that consumes the runonce settings.

A document named example.rtf is printed in this example. This example works with any type of file that has an application associated with it for printing. Most of the time you can recognize these files by the print option in the context menu when you right click the file in the Windows file explorer.

  1. Rem -- This example will show you how to create a very simple runonce configuration.
  2. Option Explicit
  3.  
  4. Dim printername
  5. Dim output, statusfile, fso, currentdir, documentfile, util, settings
  6.  
  7. Rem -- Create the COM helper object.
  8. set util = CreateObject("Pdf7.PdfUtil")
  9.  
  10. Rem -- Create the COM settings object to control the printer.
  11. set settings = CreateObject("Pdf7.PdfSettings")
  12.  
  13. Rem -- Get current path of this script.
  14. Set fso = CreateObject("Scripting.FileSystemObject")
  15. currentdir = fso.GetAbsolutePathName(".")
  16.  
  17. output = currentdir & "\out\example.pdf"
  18. statusfile = currentdir & "\out\status.ini"
  19. documentfile = currentdir & "\in\example.rtf"
  20.  
  21. Rem -- Change the value of printer name if you want to use another PDF printer
  22. printername = util.DefaultPrinterName
  23.  
  24. settings.PrinterName = printername
  25. settings.SetValue "Output", output
  26. settings.SetValue "WatermarkText", Now
  27. settings.SetValue "WatermarkColor", "#FF9900"
  28. settings.SetValue "ShowSettings", "never"
  29. settings.SetValue "ShowPDF", "no"
  30. settings.SetValue "ShowProgress", "no"
  31. settings.SetValue "ShowProgressFinished", "no"
  32. settings.SetValue "ConfirmOverwrite", "no"
  33. settings.SetValue "StatusFile", statusfile
  34. settings.SetValue "StatusFileEncoding", "unicode"
  35.  
  36. Rem -- Write settings to the runonce.ini.
  37. settings.WriteSettings True
  38.  
  39. Rem -- Remove old output and status files
  40. if fso.FileExists(output) then fso.DeleteFile(output)
  41. if fso.FileExists(statusfile) then fso.DeleteFile(statusfile)
  42.  
  43. Rem -- Print the file
  44. util.PrintFile documentfile, printername
  45.  
  46. Rem -- Wait for the status file.
  47. Rem -- The printing has finished when the status file is written.
  48. if util.WaitForFile(statusfile, 10000) then
  49.   wscript.echo "Print job finished."
  50. else
  51.   wscript.echo "Print job timed out."
  52. end if
  53.  

Downloads

Attachment Size
Example file 7.87 KB

Top