Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 681
  • Last Modified:

Print RTF File using JS.

Hi guys,

How can I print a RTF file (Word) using JavaScript.

I have a RTF file open in a Frame in a FrameSet, and now want to print that RTF file as soon as the user clicks on a Button on another Frame.

I have 70 points available, but if someone wants more points, I'll arrange it !!

Thank you very much.

Pieter
0
PieterJ
Asked:
PieterJ
  • 2
  • 2
1 Solution
 
bowkerCommented:
Pieter --

Here's cross-browser JS code that will print the contents of "document" ... so change the reference to what gets printed, then call this when the person clicks on the button:

<script language="JavaScript">
<!--

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

     function printPage() {
            if (pr) // NS4, IE5
              window.print()
            else if (da && !mac) // IE4 (Windows)
              vbPrintPage()
            else // other browsers
              alert("Sorry, your browser doesn't support this feature.");
            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>

Good luck!

Bob.
0
 
webwomanCommented:
Just to make it clear -- it only works for sure in IE, on a PC. It may not work properly if they're using Netscape, and it won't work if they use anything else.
0
 
bowkerCommented:
AFAIK, it works for NS and IE on a PC, UNIX, LINUX, etc. - Mac versions of both browsers have problems, which is why Macs are specifically excluded.  Do you know of an NS version that this won't work on ...?
0
 
webwomanCommented:
This is a FRAMED page -- so it gets a little tougher.

Also... if you're using Netscape, the file won't open anyway, you'll get prompted to download it. So printing from the browser is a moot point. ;-)
0
 
PieterJAuthor Commented:
Thanks to both of you, I see it works if the print button and the rtf document is in the same frame, but my problem is the fact that the RTF file is in another frame from where the print button is. So as soon as the RTF file opened up in the other frame, I loose control over that frame and Word has the control now. So if you use something like window.parent.docframe.print() it says that it can not find docframe.

Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now