Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

open a word document (again) 1

Status
Not open for further replies.

fb18

Programmer
Mar 4, 2005
14
GB
I am trying to open a word document in word (not in the browser). I can get it to open in the browser by using a link to the file. I tried using javascript to call a function to open word application and the specified .doc file, this didnt seem to work due to security issues with ActiveX objects (i think)

So my question is... Is there any other way i can open a word document from my perl script, i have read about using application/msword but im not really sure about how to actually implement this.

Anyone offer any advice?
 
Using the content type of application/msword would work, provided the file you're trying to send to the user always resides on the server.

Something close to the following should work:

Code:
#!/usr/bin/perl
use strict;
use CGI qw/standard/;

my $cgi = new CGI;

my $form = $cgi->Vars();

my $file = $form->{'file]};

print "Content-Type: application/msword\n";

binmode STDOUT;

open (FILE, "$file") or die "Can't open file $file: $!\n\n";

while (my $line = <FILE>)
{
    print $file;
}

close (FILE) or die "Can't close file $file: $!\n\n";

I haven't tested this code, but give it a run and see what you get.

- Rieekan
 
I tried it, I just got an internal server error,when i transfer the word doc onto the server do i have to do anything special. I just used ftp to transfer it in ascii mode. It did give me a warning saying it might not have transferred properly. Maybe that has something to do with it.?
 
Word documents are binary files, so you'll want to do the transfer in binary.

- Rieekan
 
The transfer went ok, i still get an error with the code
undefined subroutine CGI::Vars on the command line. (and internal server error in the browser) I have tried a few variations of the code but still can't get it to work. Im pretty new to Perl so at the moment it takes me a while to get my head round things.


 
My bad. Like I said, I hadn't tested the app above.

Try this:

Code:
#!/usr/bin/perl
use strict;
use CGI qw/standard/;

my $cgi = new CGI;

my $file = '/path/to/file.doc';

print "Content-Type: application/msword\n\n";

binmode STDOUT;

open (FILE, "$file") or die "Can't open file $file: $!\n\n";

while (my $line = <FILE>)
{
    print $line;
}

close (FILE) or die "Can't close file $file: $!\n\n";
[code]

 - Rieekan
 
Thanks for that, it works, however it still opens the word document in the browser and not the Microsoft word application, is there any way to launch word and show the file in the word application? (without using javascript ActiveX Object)
 
That unfortunately is something that cannot be done via straight HTML (remember, Perl is a server side language in your case and has no direct interaction with the client's computer). With the way Internet Explorer is tied to everything else in Windows, you would have to change the behaviour of the MSWord plug-in to get Word opening in a separate window or build the ActiveX Object.

- Rieekan
 
Thanks for all your help, at least i know what my options are now. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top