Click to See Complete Forum and Search --> : javascript and VBscript for printing


bloodspecter
05-28-2003, 01:02 PM
I have a little problem here. I,m pretty basic in Javascript and Iknow nothing about vbscript. Still I need some code who will be able to open a window with a friendly printing page version, print it and then close it.

I've found some code and tryed to put it like this:
---------------------------------------------
<SCRIPT LANGUAGE="VBScript">
<!--
Sub window_onunload
On Error Resume Next
Set WB = nothing
End Sub

Sub vbPrintPage
OLECMDID_PRINT = 6
OLECMDEXECOPT_DONTPROMPTUSER = 2
OLECMDEXECOPT_PROMPTUSER = 1
On Error Resume Next
WB.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER
End Sub
// -->
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
function myPrint(){
winPtr.focus();
winPtr.print();
}
</SCRIPT>

<SCRIPT LANGUAGE="JavaScript">
<!--

da = (document.all) ? 1 : 0;
pr = (window.print) ? 1 : 0;
mac = (navigator.userAgent.indexOf("Mac") != -1);

function printPage() {
winPtr = window.open("something.htm","Print");
window.setTimeout("winPtr.close()",15000);
if (pr) // NS4, IE5
myPrint();
else if (da && !mac) // IE4 (Windows)
vbPrintPage();
else // other browsers
alert("sorry, your browser don't support this function .");
return false;
}

if (da && !pr && !mac) with (document) {
writeln('<OBJECT ID="WB" WIDTH="0" HEIGHT="0" CLASSID="clsid:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>');
writeln('<' + 'SCRIPT LANGUAGE="VBScript">');
writeln('Sub window_onunload');
writeln(' On Error Resume Next');
writeln(' Set WB = nothing');
writeln('End Sub');
writeln('Sub vbPrintPage');
writeln(' OLECMDID_PRINT = 6');
writeln(' OLECMDEXECOPT_DONTPROMPTUSER = 2');
writeln(' OLECMDEXECOPT_PROMPTUSER = 1');
writeln(' On Error Resume Next');
writeln(' WB.ExecWB OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER');
writeln('End Sub');
writeln('<' + '/SCRIPT>');
}

// -->
</SCRIPT>
</head>

<body>
<A HREF="#" onClick="return printPage()">Print</A>
----------------------------------------------------------------
The part with VB is for NS who don't support window.print, while the part for mac directly write the javascript code into the document to use it. I want to make this work with the most browser possible.

I also need to know how I can focus on the new window to print it using the vbscript(I think this code is only to print the main window.) so I'll need to work it out in the mac "writein" too(That I think I can manage, but your free to help)

I know I'm asking much because of the VB but I really need help, I don,t have anything(Book, webpage) to help me.

Thank you for any help.

bloodspecter
05-28-2003, 02:04 PM
I'm so sick, forget all about the window.open, I,ll just put that code in a onload after a link is clicked....

But how would you put that code in a onload, does HTML have a onload function?