[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Scheduled windows task to Print PDF document

Posted on 2011-10-17
2
Medium Priority
?
495 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:Tonysieu
2 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 1000 total points
ID: 36978724
check wheter you have given correct username and password to Runas option in scheule
0
 

Author Comment

by:Tonysieu
ID: 36978783
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

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question