Solved

Print RTF File using JS.

Posted on 2001-07-10
5
524 Views
Last Modified: 2010-08-05
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
Comment
Question by:PieterJ
  • 2
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
bowker earned 70 total points
Comment Utility
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
 
LVL 19

Expert Comment

by:webwoman
Comment Utility
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
 
LVL 1

Expert Comment

by:bowker
Comment Utility
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
 
LVL 19

Expert Comment

by:webwoman
Comment Utility
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
 

Author Comment

by:PieterJ
Comment Utility
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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now