Wednesday, 18 March 2015

Scheduled process to print and delete of a PDF file using VBScript

Scheduled process to the print and delete of a PDF file has been already Implemented using by a batch file in my previous post, but I got the feedback from few users that the batch file does,'t work properly as it is unable to close the adobe reader editor window after the completion of the printing process. 

This post is explaining the solution for the same observation but the resolution into this post is provide through a VBScript file rather than a batch file.

Problem with batch file:
Using batch file when printing process is completed for a PDF file then there is a adobe reader editor which was used during the printing process, remains open while it should be closed to get the right impression from end users/clients.

Resolution: To achieve the right scenario, I suggest to use the VBScript file rather than a batch file because customization and code writing is lot more easier than the command line.

VBScritp sample code:
'Assignments on global variables
strPrinterName = "HP LaserJet 3055 PCL6 Class Driver" 
strPrinterDriver = "HP LaserJet 3055 PCL6 Class Driver" 
strPrinterPort = "IP_172.16.16.103"
strAcroRead = "C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe" 
strPDFFolder="D:\san"
strPDFFile="pdf-sample.pdf"
intSleepTime=15000

dim filesys 

'Get the file object
Set filesys = CreateObject("Scripting.FileSystemObject")  

If Not filesys.FileExists(strPDFFolder & "\" & strPDFFile) Then 
WScript.Quit 
End If

'Get an object of WScript
Set oShell = CreateObject("WScript.Shell")

'Integration of folder, files and printer detail

oShell.Run Chr(34) & strAcroRead & Chr(34) & " /t  "  _ 
    & Chr(34) & strPDFFolder & "\" & strPDFFile  & Chr(34) & " " _ 
    & Chr(34) & strPrinterName & Chr(34) & " " & Chr(34) & strPrinterDriver & Chr(34) & " " & Chr(34) & strPrinterPort & Chr(34)

'Printing of the file will be completed at this point


'After allowing few seconds for spooling print job
WScript.Sleep 15000

'Delete the file from particular folder
'Check if file is existing
If filesys.FileExists(strPDFFolder & "\" & strPDFFile) Then 
'Delete the file 
filesys.DeleteFile strPDFFolder & "\" & strPDFFile 
End If

'May be there is an open application of adobe reader
'Check if there is an open instance of adobe reader then it should be closed
strComputer = "." 
Set objWMIService = GetObject _ 
    ("winmgmts:\\" & strComputer & "\root\cimv2") 
Set colProcessList = objWMIService.ExecQuery _ 
    ("Select * from Win32_Process Where Name = 'AcroRd32.exe'") 
For Each objProcess in colProcessList 
   objProcess.Terminate() 

Next 

Assumption:
   1. The above requirement has been analyzed on 32 bit Microsoft windows operating system.
   2.  There is only one pdf file, above methodology will not be supporting in case of multiple pdf files.

Mandatory items: There are following items must be available into the respective computer system.
   1.     Adobe reader must be installed to read the PDF file.
   2.     Printer must be configured into the source laptop to capture the print command.

Input Required:
   1.     Adobe reader exe location (Example- C:\Program Files (x86)\Adobe\Reader 11.0\Reader>AcroRd32.exe )
   2.     Subscribed SSRS pdf report location which is required to print on scheduled basis.
   3.     Printer name, Printer driver, Printer Port

Process flow:
   1.     Get the deployed SSRS report into PDF format using report subscription functionality
   2.     Print the same SSRS pdf report using command line utility which is a batch file

   3.     Schedule the batch file execution using Windows Task Scheduler or SQL Job Agent.

Windows Task Scheduler - Scheduled printing on single SSRS PDF file

Follow the below steps to configure the Windows Task Scheduler
1.     Open Task Scheduler by clicking the Start button, clicking Control Panel, clicking System and Security, clicking Administrative Tools, and then double-clicking Task Scheduler.‌if you're prompted for an administrator password or confirmation, type the password or provide confirmation.


2. Click the Action menu, and then click Create Basic Task.

3.     Do one of the following:
a.     To select a schedule based on the calendar, click Daily, Weekly, Monthly, or One time, click Next; specify the schedule you want to use, and then click Next.
b.     To select a schedule based on common recurring events, click When the computer starts or When I log on, and then click Next.
c.      To select a schedule based on specific events, click When a specific event is logged, click Next; specify the event log and other information using the drop-down lists, and then click Next.


4.     To schedule a program to start automatically, click Start a program, and then click Next.

5. Click Browse to find the program you want to start, and then click Next.


6. Click Finish.

I believe this post will help to close the adobe reader editor after completion of the printing process.

See also:

2 comments:

  1. Hi,
    Nice works, but how can you print multiple files from 1 folder?

    Thanks, Rio

    ReplyDelete
    Replies
    1. There must be some loop facility, I never tried on it.

      Delete