我面临的问题是,当我以我的身份运行时,我的代码可以工作,但以域帐户的身份运行时,我的代码无法工作。我正在从文件位置打开.mht文件,然后尝试打印。打开文件即可,并且可以正常显示页面,但无法打印。到目前为止,这是我的代码:
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)
此时会引发错误
远程过程调用失败。 (来自HRESULT的异常:0x800706BE)
我已经尝试过声明为各种对象类型,但是无济于事,甚至尝试从打开的浏览器中进行打印。当我这样做时,它无法打印,并且出现一条消息,指出该网页有问题并已重新加载。如果我导航到一个互联网站点并尝试打印,则同样会缺少打印和消息。
我不确定需要尝试解决什么问题。可以是域帐户的凭据吗?是否需要在特定的用户组中?这是打印机安全性问题吗?我已经将头撞在砖墙上了大约一个星期,而且我没有前进的脚步,因此非常感谢您的帮助。
抱歉,对于延迟的回复,自上周五以来我一直不在办公室。这是完整的代码。
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
您将(可能需要)在项目中引用以下内容:
Interop.SHDocVw.dll
Interop.PrintUIObjLib.dll
System.Printing.dll
创建一个模块并添加以下内容
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
如果您需要其他帮助,请告诉我。