What do I need to look into to fix this printing i

2019-08-07 11:31发布

问题:

The problem I am facing is that my code works when I run as me but doesn't work when I run as a domain account. I'm opening an .mht file from a file location and tring to print it. Opening the file works and it displays the page fine but printing it doesn't work. Heres my code so far:

    Dim ie As SHDocVw.InternetExplorerMedium = New SHDocVw.InternetExplorerMedium
    AddHandler ie.PrintTemplateTeardown, AddressOf PrintedCB
    AddHandler ie.DocumentComplete, AddressOf LoadedCB
    ie.Visible = True
    WriteLog("Internet Explorer made visible")
    ie.Navigate(URL)
    Application.DoEvents()
    While Not documentLoaded
        WriteLog("Waiting for Internet Explorer to load page")
        Application.DoEvents()
        Threading.Thread.Sleep(1000)
    End While
    WriteLog("Internet Explorer navigated to URL")
    Application.DoEvents()
    ie.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, PRINT_WAITFORCOMPLETION, 0)

At this point an error is thrown

The remote procedure call failed. (Exception from HRESULT: 0x800706BE)

I've already tried declaring ie as various object types but to no avail and even tried printing from the browser that opens. When I do this it fails to print and a message comes up to say the web page had a problem and has been re-loaded. If I navigate to a an internet site and try to print I get the same lack of print and message.

I'm not sure what I need to look into to try and resolve the issue. Could it be the credentials of the domain account? Does it need to be in a specific user group? Is it a printer security issue etc? I've been banging my head against a brick wall for about a week now and I'm no further forward so would really appreciate some assistance.

Sorry, for the delayed reply, I was out of office since last Friday. Here is a complete piece of code.

    Public Class Browser
        Private documentLoaded As Boolean = False
        Private documentPrinted As Boolean = False
        Public Function PrintMHTMLFile(ByVal blnShowPrompt As Boolean, URL As String) As Boolean
            PrintMHTMLFile = False
            Dim RetVal As Boolean = False
            Const PRINT_WAITFORCOMPLETION = CShort(2)   'use this setting to tell ExecWB to wait to return
            Dim ie As SHDocVw.InternetExplorerMedium = New SHDocVw.InternetExplorerMedium
            AddHandler ie.PrintTemplateTeardown, AddressOf PrintedCB
            Debug.print(WriteLog("Internet Explorer started")
            Try
                ie.Visible = True
                Debug.print("Internet Explorer made visible")
                ie.Navigate(URL)
                Application.DoEvents()
                While Not documentLoaded
                    Debug.print("Waiting for Internet Explorer to load page")
                    Application.DoEvents()
                    Threading.Thread.Sleep(10000)
                End While
                Debug.print("Internet Explorer navigated to URL")
                Application.DoEvents()
                ie.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, PRINT_WAITFORCOMPLETION, 0)
                While Not documentPrinted
                    debug.print("Waiting for Internet Explorer to print page")
                    Threading.Thread.Sleep(1000)
                End While
                Debug.Print("Internet Explorer printed URL")
                ie.ExecWB(SHDocVw.OLECMDID.OLECMDID_CLOSE, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER)
                RetVal = True
                Debug.print("Internet Explorer closed")
            Catch ex As Exception
                Debug.print("Unable to print " & URL)
                Debug.print("Internet Explorer errored with error message """ & ex.Message & """")
                ie.ExecWB(SHDocVw.OLECMDID.OLECMDID_CLOSE, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER)
            Finally
                ie = Nothing
            End Try
            return Retval
        end Function
        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
    End Class

You will(may?) need references in the project to:

Interop.SHDocVw.dll
Interop.PrintUIObjLib.dll
System.Printing.dll

Create a module and add the following

    Sub Printit()
        Dim browser As New Browser
        Dim URL as string = "www.google.com" 'Substitute with a location you want to print
        browser.PrintMHTMLFile(URL)
    end sub

Let me know if you need anything more, thanks for the offer of help.