当我尝试多次打印 COM 网络浏览器时,我遇到了问题。
对于复制问题:
1)创建一个新的“Windows 窗体”项目
2)添加对“Microsoft Internet 控件”的 COM 引用
3)将 webbrowser 控件“webbrowser1”和按钮“button1”添加到表单(来自工具箱)
4)确保您有文件“c: \index.html"
5) 添加此代码...
Option Explicit On
Imports System.IO
Imports System.Reflection
Imports System.Diagnostics.Process
Imports System.Runtime.InteropServices
Imports SHDocVw
Public Class Form1
Dim WithEvents p As New PrintHTML
Dim htmlfilename As String
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
p = Nothing
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
htmlfilename = "c:\index.html"
WebBrowser1.Navigate(htmlfilename)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
p.PrintHTMLDocument(htmlfilename)
End Sub
End Class
Public Class PrintHTML
Dim documentLoaded As Boolean = False
Dim documentPrinted As Boolean = False
Public Sub PrintHTMLDocument(ByVal htmlfilename As String)
Dim ie As New InternetExplorer
AddHandler DirectCast(ie, InternetExplorer).PrintTemplateTeardown, AddressOf PrintedCB
AddHandler DirectCast(ie, InternetExplorer).DocumentComplete, AddressOf LoadedCB
ie.Navigate(htmlfilename)
While Not documentLoaded AndAlso ie.QueryStatusWB(OLECMDID.OLECMDID_PRINT) <> OLECMDF.OLECMDF_ENABLED
Application.DoEvents()
Threading.Thread.Sleep(100)
End While
Try
ie.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, vbNull, vbNull)
While Not documentPrinted
Application.DoEvents()
Threading.Thread.Sleep(100)
End While
Catch ex As Exception
Debug.Print(ex.Message)
End Try
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
End Class
当我第一次单击 button1 时,一切都按预期运行(打印开始),但是当我单击 button1 进行多次打印时 - 而不是打印,我收到错误消息:
my.exe 中出现“System.Runtime.InteropServices.COMException”类型的第一次机会异常
尝试撤销尚未注册的放置目标(HRESULT 异常:0x80040100 (DRAGDROP_E_NOTREGISTERED))
什么可能导致此错误,我怎样才能摆脱它以便能够使用描述的组件多次打印文档?