Hello,
I have programming problem which I don't know how to solve so I need help.
My program have (among others) one webbrowser control on it for showing html.
Under click event of button "Print" I need to create totally NEW web browser, load actual document and print it then kill those newly created webbrowser.
With showed code I get error: Error HRESULT E_FAIL has been returned from a call to a COM component.
How to write explained code properly and get my function to work?
I have programming problem which I don't know how to solve so I need help.
My program have (among others) one webbrowser control on it for showing html.
Under click event of button "Print" I need to create totally NEW web browser, load actual document and print it then kill those newly created webbrowser.
With showed code I get error: Error HRESULT E_FAIL has been returned from a call to a COM component.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
documentLoaded = False
documentPrinted = False
Dim ie As New InternetExplorer
'Dim ie As New SHDocVw.WebBrowser
AddHandler DirectCast(ie, SHDocVw.WebBrowser).PrintTemplateTeardown, AddressOf PrintedCB
AddHandler DirectCast(ie, SHDocVw.WebBrowser).DocumentComplete, AddressOf LoadedCB
ie.Navigate(htmlfilename)
While Not documentLoaded AndAlso ie.QueryStatusWB(olecmdid.OLECMDID_PRINT) <> OLECMDF.OLECMDF_ENABLED
Threading.Thread.Sleep(100)
End While
Try
ie.ExecWB(olecmdid.OLECMDID_PRINT, execopt.OLECMDEXECOPT_PROMPTUSER, vbNull, vbNull)
While NOT documentPrinted
Threading.Thread.Sleep(100)
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
RemoveHandler DirectCast(ie, SHDocVw.WebBrowser).PrintTemplateTeardown, AddressOf PrintedCB
RemoveHandler DirectCast(ie, SHDocVw.WebBrowser).DocumentComplete, AddressOf LoadedCB
ie.Quit()
ie = Nothing
End Sub
Private Sub LoadedCB(ByVal obj As Object, ByRef url As Object)
documentLoaded = True
End Sub
Private Sub PrintedCB(ByVal obj As Object)
documentPrinted = True
End Sub