Webbrowser printing

E

eskildb

First, please be gently. I am fairly new to the programming world (1.5
years with some expermentation prior to).

I have been working on a project that has to print HTML pages with
graphics in a unattended automated fashion. I have a webbrowser that
is created with code but not seen. I found the below code on the
internet. It creates a webbrowser, and specifies the URL, which
navigates the webbrowser to the page. When the document is finished
loading, the second sub kicks in to print it. This has worked great up
to this point. Now I am know trying to catch errors/exceptions with no
luck. The code seems to just keep going. Examples of exceptions to
catch
1.) Pass a network URL that can not be reached. Currently the program
accepts the url, I assume 404 file not found problably loads and
completes, which kicks of the PrintDocument sub. No errors with the
current code.
2.) Pass a URL that can be reached and loaded properly. The
PrintDocument sub is run, but say a printer error occurs. No errors
with the current code.

Anybody with any ideas on how to catch errors?

Should I be using a different method for printing? If so, sample code
would be appreciated. I have seen other code to create a HTML doc to
load and then print with the EXECWB command, but have had not luck
getting it to work.

*************************************************************************
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
*************************************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top