首先在HTML 页面中加入WebBrowser 的Object:
<OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height=0 id=wb name=wb width=0></OBJECT>
之后,使用如下代码进行打印:
<script language="VBScript">
sub printvb()
wb.execwb 6,2,3
End Sub
</script>
关键代码 wb.execwb 6,2,3 可以在 VBScript 方式下实际无需提示的直接打印,但奇怪的是用同样的参数,在 Javascript 方式下IE 依然会弹出打印对话窗口,也许是因为VBScript 是Microsoft 开发的专用于 IE 的吧,而 Javascript 毕竟是外人:)
另找到一个较全面的例子,如下:
6=OLECMDID_PRINT
2=OLECMDEXECOPT_DONTPROMPTUSER,
2+1=PRINT_WAITFORCOMPLETION + PRINT_DONTBOTHERUSER
WB.ExecWB 6, 2, 3, 0
<HTML>
<HEAD>
<OBJECT ID="WB" WIDTH="0" HEIGHT="0" CLASSID="clsid:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>
<SCRIPT LANGUAGE="VBScript">
Sub window_onunload
On Error Resume Next
Set WB = nothing
End Sub
Sub vbPrintPage ( x , y , z )
OLECMDID_PRINT = 6
OLECMDEXECOPT_DODEFAULT = 0
OLECMDEXECOPT_PROMPTUSER = 1
OLECMDEXECOPT_DONTPROMPTUSER = 2
On Error Resume Next
WB.ExecWB x, y, z, 0
End Sub
</SCRIPT>
</HEAD>
<BODY>
<TABLE BORDER=1><TR><TD>
<INPUT TYPE="BUTTON" VALUE="Print"><BR>
<INPUT TYPE="BUTTON" VALUE="Preview"><BR>
<INPUT TYPE="BUTTON" VALUE="Page Setup"><BR>
</TD>
<TD bgcolor="green">Print Test
</TD></TR></TABLE>
</BODY>
</HTML>