Scheduled windows task to Print PDF document

Hi All,
I created a scheduled windows task (run every 5 minutes) which call the below function to print out PDF documents.

My scheduled task runs and prints out the PDF documents perfectly when configured  "Run only when the user is logged in".  When I configured "Run Task Whether The User is Logged In or Not", the PDF didnt get printed.

Can anyone please show me how do modify my code so that the task can run and print out PDF document whether the user is logged in or not.  

Kindly Appreciate




Public Shared Sub PrintReport(ByVal filePathToPrint As String, ByVal printerName As String, ByRef isExecutionSuccess As Boolean)
        'ToDo Phong: check if the printer is online
        Dim Timeout As Integer = 10
        Dim Proc As New System.Diagnostics.Process


        Try
            If (File.Exists(filePathToPrint)) Then
               

                If printerName Is Nothing OrElse printerName.Trim.Length = 0 Then
                    printerName = (New System.Drawing.Printing.PrinterSettings).PrinterName
                End If

                Proc.EnableRaisingEvents = True
                Proc.StartInfo.FileName = filePathToPrint
                Proc.StartInfo.Arguments = Chr(34) + printerName + Chr(34)
                Proc.StartInfo.Verb = "PrintTo"
                Proc.StartInfo.WindowStyle = ProcessWindowStyle.Minimized
                Proc.StartInfo.CreateNoWindow = True
                Proc.Start()

                Do While Timeout > 0 AndAlso Not Proc.HasExited
                    System.Threading.Thread.Sleep(1000)
                    Timeout -= 1
                Loop

                If Not Proc.HasExited Then
                    'Debug.Print("Killing process")
                    Proc.Kill()
                End If

                'Debug.WriteLine("Closing process")
                Proc.Close()
                isExecutionSuccess = True

            Else
                Throw New ReportGeneratorException(String.Format(Resources.My.Resources.ErrorMessages.ETG001, filePathToPrint, Environment.NewLine, Environment.MachineName))
            End If
        Catch ex As Exception
            isExecutionSuccess = False
            Throw
        End Try
    End Sub
TonysieuAsked:
Who is Participating?
 
Pratima PharandeCommented:
check wheter you have given correct username and password to Runas option in scheule
0
 
TonysieuAuthor Commented:
The password is set correctly.

When run with "user logged in", I can see Adobe Reader open then the PDF document gets printed out.

When run with "whether the user is logged in or not", I can not see Adobe Reader open and report didnt come out.

The below is the entire code of my console app. I created a windows scheduled task which call the console exe file. Any other ideas?


Imports System.IO

Module Module1

    Sub Main()
        'C:\TEMP\test.PDF
        PrintReport("C:\TEMP\test.PDF", String.Empty, False)

    End Sub


    Public Sub PrintReport(ByVal filePathToPrint As String, ByVal printerName As String, ByRef isExecutionSuccess As Boolean)
        'ToDo Phong: check if the printer is online
        Dim Timeout As Integer = 10
        Dim Proc As New System.Diagnostics.Process


        Try
            If (File.Exists(filePathToPrint)) Then


                If printerName Is Nothing OrElse printerName.Trim.Length = 0 Then
                    printerName = (New System.Drawing.Printing.PrinterSettings).PrinterName
                End If

                Proc.EnableRaisingEvents = True
                Proc.StartInfo.FileName = filePathToPrint
                Proc.StartInfo.Arguments = Chr(34) + printerName + Chr(34)
                'Proc.StartInfo.Verb = "PrintTo"
                Proc.StartInfo.Verb = "Print"
                Proc.StartInfo.WindowStyle = ProcessWindowStyle.Minimized
                Proc.StartInfo.CreateNoWindow = True
                Proc.Start()

                Do While Timeout > 0 AndAlso Not Proc.HasExited
                    System.Threading.Thread.Sleep(1000)
                    Timeout -= 1
                Loop

                If Not Proc.HasExited Then
                    'Debug.Print("Killing process")
                    Proc.Kill()
                End If

                'Debug.WriteLine("Closing process")
                Proc.Close()
                isExecutionSuccess = True

            Else
                'Throw New ReportGeneratorException(String.Format(Resources.My.Resources.ErrorMessages.ETG001, filePathToPrint, Environment.NewLine, Environment.MachineName))
            End If
        Catch ex As Exception
            isExecutionSuccess = False
            Throw
        End Try
    End Sub
End Module
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.