I have been working on a project to print sheets in a unattended
automated fashion. I found the below code (section 3) on the internet.
I works great, but with limitations. It used the defaults of IE
(Default system printer, footers, headers, etc.). The problem I am
having is trying to catch errors/exceptions. The code seems to just
keep going. Anybody with any ideas on how to catch errors.
To print to different printers, you have to change the system default
printer (see below section 1).
To change a footer, you have to modify a reg key (see below section 2).
********************************************************************************
Get and Set printer
Public Function GetDefaultSystemPrinter() As String
'Get the system default printer
Try
Dim pd As New Printing.PrintDocument
Return pd.PrinterSettings.PrinterName
Catch ex As Exception
End Try
End Function
http://www.planet-source-code.com/U...Code!asp/txtCodeId!3053/lngWid!10/anyname.htm
********************************************************************************
Footer Code
Public Sub SetIEFooter(ByVal NewFooter As String)
'MS Article ID : 311280
'unable to set CurrentUser RegKey with Service because service
does not know who is current user
Try
Dim strKey As String = "Software\Microsoft\Internet
Explorer\PageSetup"
Dim bolWritable As Boolean = True
Dim strName As String = "footer"
Dim oValue As Object = NewFooter
'original footer value = &u&b&d
Dim oKey As RegistryKey =
Registry.CurrentUser.OpenSubKey(strKey, bolWritable)
oKey.SetValue(strName, oValue)
oKey.Close()
Catch ex As Exception
Dim strErrorMsg As String
strErrorMsg = "Error in SetIEFooter-PrintHTML." & vbCrLf &
ex.ToString
'add LogMessage here
MessageBox.Show(strErrorMsg)
Dim clsEmail As New Email
clsEmail.EmailSend(strErrorEmailName & strEmailDomian,
SystemInformation.ComputerName & strEmailDomian, _
"An Error Message From " &
Application.ProductName, strErrorMsg)
End Try
End Sub
********************************************************************************
Browser print code
Public Sub LoadBrowser(ByVal strFileURL As String)
Try
'Create a WebBrowser instance.
Dim webBrowserForPrinting As New WebBrowser()
'PrintInProgress = True
Printing = "True"
'Add an event handler that prints the document after it
loads.
AddHandler webBrowserForPrinting.DocumentCompleted, New _
WebBrowserDocumentCompletedEventHandler(AddressOf
PrintDocument)
'Set the Url property to load the document.
webBrowserForPrinting.Url = New Uri(strFileURL)
Catch ex As Exception
Dim strErrorMsg As String
strErrorMsg = "Error in LoadBrowser-PrintHTML." & vbCrLf &
ex.ToString
Printing = "Error in LoadBrowser-PrintHTML." & vbCrLf &
ex.Message
'add LogMessage here
MessageBox.Show(strErrorMsg)
Dim clsEmail As New Email
clsEmail.EmailSend(strErrorEmailName & strEmailDomian,
SystemInformation.ComputerName & strEmailDomian, _
"An Error Message From " &
Application.ProductName, strErrorMsg)
End Try
End Sub
**************
Public Sub PrintDocument(ByVal sender As Object, _
ByVal e As WebBrowserDocumentCompletedEventArgs)
Try
If Microsoft.VisualBasic.Left(Printing, 5) = "Error" Then
Else
Dim webBrowserForPrinting As WebBrowser = CType(sender,
WebBrowser)
'Print the document now that it is fully loaded.
webBrowserForPrinting.Print()
'Dispose the WebBrowser now that the task is complete.
webBrowserForPrinting.Dispose()
Printing = "False"
End If
Catch ex As Exception
Dim strErrorMsg As String
strErrorMsg = "Error in PrintDocument-PrintHTML." & vbCrLf
& ex.ToString
Printing = "Error in PrintDocument-PrintHTML." & vbCrLf &
ex.Message
'add LogMessage here
MessageBox.Show(strErrorMsg)
Dim clsEmail As New Email
clsEmail.EmailSend(strErrorEmailName & strEmailDomian,
SystemInformation.ComputerName & strEmailDomian, _
"An Error Message From " &
Application.ProductName, strErrorMsg)
End Try
End Sub