Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27554

VS 2008 New instance of webbrowser

$
0
0
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.

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

How to write explained code properly and get my function to work?

Viewing all articles
Browse latest Browse all 27554

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>