|
Question : Printing from .NET Windows Service - Object reference not set to an instance of an object
|
|
I am modifying a .NET Windows Service in VS .NET 2003 on Windows XP that sweeps a database for address data and prints a PDF letter from a template. I am getting the error, "Object reference not set to an instance of an object" from the function that prints the PDF letter. The code is shown below. The error is being generated from the AddToEventLog after the Catch. Would appreciate any help yall can provide. I am a newbie at .NET programming and did not actually write this service but have been assigned to modify it. It currently works in production on a Windows 2000 machine.
Thanks in advance Annie
Private Sub printPDFFile(ByVal strOutputFile As String) Dim objStartInfo As New ProcessStartInfo Dim objProcess As New System.Diagnostics.Process
If IsValidPrinter() Then ' set start info properties With objStartInfo .CreateNoWindow = True .FileName = AcrobatReaderExecutable .Arguments = "/t " & strOutputFile & " " & ServicePrinterName .UseShellExecute = True .WindowStyle = ProcessWindowStyle.Hidden .ErrorDialog = True '.RedirectStandardError = True End With
' start process Try objProcess = Process.Start(objStartInfo) objProcess.Close()
' print to event log AddToEventLog("Attempting to print generated " & getWordString() & " letter '" & strOutputFile & "' to printer [" & ServicePrinterName & "]", EventLogEntryType.Information) Catch ex As Exception ' error occurred AddToEventLog("Error encountered while attempting to print PDF file to printer [" & ServicePrinterName & "] " & vbCrLf & ex.Message & vbCrLf & ex.Source & vbCrLf & ex.TargetSite.Name, EventLogEntryType.Error) End Try Else ' printer not valid AddToEventLog("The file '" & strOutputFile & "' cannot be printed. The printer [" & ServicePrinterName & "] either is not valid or has not been setup as a printer on this machine.", EventLogEntryType.Error) End If
End Sub
|
|
Answer : Printing from .NET Windows Service - Object reference not set to an instance of an object
|
|
Just to clarify: You don't necessarily want to shut it down if it's already running because presumably it's running for a reason. The code I posted above will only shut the process down if it was started by your code.
|
|
|