473,414 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Error in opening a word document from a web application

Hi,
I have to open a word document from my aspx page.The Page_Load method
contains the following code

System.IO.TextWriter tx = Response.Output;
System.IO.StreamReader sr = new System.IO.StreamReader(this.Path);
Response.ContentType = "application/msword";
tx.Write(sr.ReadToEnd());
sr.Close();

Just to give more information, I have a First.aspx page which contains
a link to the Second.aspx page passing the complete path value. The
Page_Load method of Second.aspx page was shown above.
If I click the link, FileOpen dialog comes up and asks me for open,save
and cancel options. If I click Open, Microsoft Word opens and greets me
with a message: "Microsoft word needs a converter to display this file
correctly".If I click Cancel, then it is asking for me to select an
encoder.
What is the error? Am I doing the right thing in the Page_Load method.
I have MS Office 2003 installed and my IIS version is 5.1

I would be grateful for any help

Jan 23 '07 #1
4 2121
Sunil,

I think you are not writting the word document to the Response object. Pls
have a look at the sample code which i'v done before ...

string name = "your filename.doc";
Response.ClearHeaders();

Response.ClearContent();

Response.ContentType = "application/vnd.ms-word";

Response.AppendHeader( "content-disposition","inline;filename=" + name );

Response.WriteFile(Server.MapPath("name"));
// taking into consideration your word is in the root application folder
Response.End();

The above code worked for me
--
Thanks & Regards,
Mark Nelson
"sunil" wrote:
Hi,
I have to open a word document from my aspx page.The Page_Load method
contains the following code

System.IO.TextWriter tx = Response.Output;
System.IO.StreamReader sr = new System.IO.StreamReader(this.Path);
Response.ContentType = "application/msword";
tx.Write(sr.ReadToEnd());
sr.Close();

Just to give more information, I have a First.aspx page which contains
a link to the Second.aspx page passing the complete path value. The
Page_Load method of Second.aspx page was shown above.
If I click the link, FileOpen dialog comes up and asks me for open,save
and cancel options. If I click Open, Microsoft Word opens and greets me
with a message: "Microsoft word needs a converter to display this file
correctly".If I click Cancel, then it is asking for me to select an
encoder.
What is the error? Am I doing the right thing in the Page_Load method.
I have MS Office 2003 installed and my IIS version is 5.1

I would be grateful for any help

Jan 23 '07 #2

Mark Nelson wrote:
Sunil,

I think you are not writting the word document to the Response object. Pls
have a look at the sample code which i'v done before ...

string name = "your filename.doc";
Response.ClearHeaders();

Response.ClearContent();

Response.ContentType = "application/vnd.ms-word";

Response.AppendHeader( "content-disposition","inline;filename=" + name );

Response.WriteFile(Server.MapPath("name"));
// taking into consideration your word is in the root application folder
Response.End();

The above code worked for me
--
Thanks & Regards,
Mark Nelson

I am writing into the Response object using a text writer

System.IO.TextWriter tx = Response.Output;
System.IO.StreamReader sr = new System.IO.StreamReader(this.Path);
Response.Clear();
Response.ContentType = "application/msword";
tx.Write(sr.ReadToEnd());
sr.Close();

The same thing works for other files like htm,txt files with
Response.ContentType set properly.
The problem using your code is that my file is not on IIS. So I cannot
use Server.Map()
The path that I refer is a complete path to a Word document in the file
system of the local machine
Any suggestions?
Thanks for the response

Jan 23 '07 #3
I am writing into the Response object using a text writer

That might be your problem? A word file isn't a text file, try a
StreamWriter instead.
The problem using your code is that my file is not on IIS. So I cannot
use Server.Map()
Use a normal path then;

Response.WriteFile(@"c:\somefolder\somefile.doc");
Jan 23 '07 #4

Aidy wrote:
I am writing into the Response object using a text writer

That might be your problem? A word file isn't a text file, try a
StreamWriter instead.
The problem using your code is that my file is not on IIS. So I cannot
use Server.Map()

Use a normal path then;

Response.WriteFile(@"c:\somefolder\somefile.doc");
Response.WriteFile worked for me. Initially, I had thought that it
works only for files on the server.
Thanks for such a tip.

Jan 23 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andy Davis | last post by:
I have set up a mail merge document in Word 2003 which gets its data from my Access 2000 database. I want to set up a button on a form that: 1. runs the query to provide the dat for the merge...
1
by: Elizabeth Harmon | last post by:
Hi All I am attempting to open a Word App from a web page and so far everything works (After reconfig of dcomcnfg for Microsoft Word Document). I have one minor problem, i cannot get the...
0
by: Sivaraman.S | last post by:
Issue : When I am trying to open a Word Document or a Template from a Web Application (ASP.NET Web page - Language Used VB.NET Visual Studio Version 2003) using Word.ApplicationClass (Refering...
7
by: mkorch17 | last post by:
Hello, I have a server with a Word document and an Access database on it. I also have an ASP page running on the server. The Word document contains a mail merge with the Access database. What...
2
by: Colin Halliday | last post by:
I have a Word 2003 mail merge main document (form letter) that is linked to another Word document data source for the mail merge. If I open this doc using the Word GUI, it first asks me to...
5
by: Cedric | last post by:
Hi Folks, I'm wondering if someone has already succeed to open a word2000 document located on server side with javascript code executed by a web browser on client side. In fact, i make it...
3
by: Csaba Gabor | last post by:
If I do not have the indicated Word document open (on my Win XP Pro machine with PHP 5.2), the following will open and display it: <?php $path = "c:\\path\\to\\word\\document\\mydoc.doc"; $doc...
4
by: Willis Wilson | last post by:
Platform: Access 2003 SP2 and Windows XP. I've tried a number of different scenarios and none of them seem to work The "OpenDataSource" statement gives a Run-time error 5922. Please help find...
0
by: =?Utf-8?B?V2FubmFiZQ==?= | last post by:
I am creating a webpage and opening it as a Word document. Problem is that as I create one of the questions, some of the text is not appearing in the document as it should. As I step through it, I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.