• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1200
  • Last Modified:

Create a Word document using ASP on Server.

Hi,

Following is the requirement.

1. Need to extract the details from oracle database.
2. prepare a word document. Word document should have headers and footers. where header can be a simple message like 'Internal Use only' and footer needs to have Page nos.
3. Display the generated word document on browser. Condition: Once the word is generated 'File Download' with Open, Save, Cancel should not be displayed.

Please provide the solution for the 2nd and 3rd points mentioned above. Headers and Footers, Should not display File Download.

Regards,
Mahesh.
0
thotamahesh
Asked:
thotamahesh
  • 8
  • 6
  • 3
2 Solutions
 
hujiCommented:
Let's break it down to two subsections:
1) The word file
You can create an empty template of it in Word. Instead of the text you want to put in the file, use AAAAAAAAAAAA. Save it as XML (File > Save As) and then open it with ASP, replace() the AAAAAAAAAAAAAA with the required text and save it again but this time with .doc extension. It will open correctly in Word.
This might help too:
http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_20456507.html
2) The file being sent to the user:
You need to use the correct content-type. If you use application/msword that download box shouldn't appear "in Internet Explorer". Other browsers will still show it.
I'll try to send some code soon.
Huji
0
 
hujiCommented:
Here is the ASP file I created for the very basic example:

<%
Const WordNewLine = "</w:t></w:r></w:p><w:p/><w:p><w:r><w:t>"
Set fs=Server.CreateObject("scripting.filesystemobject")
Set f = fs.OpenTextFile(Server.MapPath("Template.xml"),1,True)
result = f.ReadAll
f.Close
result = Replace(result,"FIRSTROW","The first row of text is here")
result = Replace(result,"SECONDROW", "The second row of text is here" & WordNewLine & "And the thrid")
Response.ContentType = "application/msword"
Response.Write result
%>

And you can download the Template.xml file from this link below:

https://filedb.experts-exchange.com/incoming/ee-stuff/201-Template.zip

You will need to unzip it.
0
 
hujiCommented:
There are other options too, like using HTML: http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_21034208.html However, I'm afraid they don't support "page numbers" while my method does.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
thotamaheshAuthor Commented:
I am trying with the above options but I have a Tables and in some table cells backround color needs to filled with colour also. So the Replace function you have given will work for creating tables?
0
 
thotamaheshAuthor Commented:
With the following code.

Dim fs,f,result
Const WordNewLine = "</w:t></w:r></w:p>This is just for testing<w:p/><w:p><w:r><w:t>"
Set fs=Server.CreateObject("scripting.filesystemobject")
Set f = fs.OpenTextFile(Server.MapPath("MonthReport.xml"),1,True)
result = f.ReadAll
f.Close
result = Replace(result,"FIRST","Start" & WordNewLine & "end")
Response.ContentType = "application/msword"
Response.Write result

I got the below result.

StartThis is just for testingendmahesh.thotamahesh.thota132006-06-12T09:19:00Z2006-06-12T09:22:00Z105Reuters11511.6502StartThis is just for testingend .


Please suggest how to remove the "testingendmahesh.thotamahesh.thota132006-06-12T09:19:00Z2006-06-12T09:22:00Z105Reuters11511.6502" and also not repeat the same text again as mentioned above.
"StartThis is just for testingend"


Please suggest to proceed further.
0
 
thotamaheshAuthor Commented:
If the multiple users requested then will the above solution will work?

and also

After the content displayed on browser. When I try to save by clicking on 'File -> Save AS 'I am not getting the Word Document in the file type. Is it possible to save the content displayed browser to a Word Document?

Please update me.

 
0
 
thotamaheshAuthor Commented:
Now I am modifying my 3rd point.

3. Once the word document is generated. If 'Always ask before opening this type of file' checkbox is checked then File Download option should be displayed. If selected on Open or checkbox is unchecked then word document should be displayed directly on browser as a Word. User can then edit the doc, save or can print the doc.
if user clicked on Save button in the File Download option then user can save the document...by default the project name should be set for the file and this value i will extact from database.

Now please suggest me how I can develop? As mentioned earlier I need Headers and Footers. with Headers having a message and Fotters with Page Nos.



0
 
hujiCommented:
Wow! Lots of comments from your side! I had to read them twice. Now here is the answer:
A) I didn't know how complex your output in the word file is going to be. I thought you are going to simply show some text in it, so I suggested the most simple solution, which is the XML and Replace trick. However, if you are going to have tables, with background, then I would suggest the HTML method, for which I have sent you a link. {http:#16881663} However, I'm afraid it won't support Header and Footers. (The XML method supports all, but I'm afraid we can't learn that XML formatting used by Word totally.)
I will do some more searches, and will inform you.
Regards,
Huji
0
 
joeposter649Commented:
3.
Use this to do a binary stream of the file...
http://support.microsoft.com/?kbid=276488

Then modifiy it to send the content-disposition header with "inline" or "attachemnt"...
http://www.aspfaq.com/search.asp?q=content-disposition
0
 
hujiCommented:
Well, regarding that thotamahesh needs the file to be opened in Internet Explrorer itself, I think content-disposition should be set to inline, and not to attachment (which will open MS Word itself to show the word file.)
0
 
joeposter649Commented:
<<Well, regarding that thotamahesh needs the file to be opened in Internet Explrorer>>
See "Now I am modifying my 3rd point."...
http://www.experts-exchange.com/Web/Web_Languages/ASP/Q_21882346.html#16886644
0
 
thotamaheshAuthor Commented:
For Headers and Footers I am using the below code found out some where but I could not still achieve the result.

You need to create a header/footer file, add the lines bellow to the STYLES code section, and set a <DIV id=section1>

/* Page Definitions */
@page
{mso-footnote-separator:url("<%=Application("ServerPath")%>header.htm") fs;
mso-footnote-continuation-separator:url("<%=Application("ServerPath")%>header.htm") fcs;
mso-endnote-separator:url("<%=Application("ServerPath")%>header.htm") es;
mso-endnote-continuation-separator:url("<%=Application("ServerPath")%>header.htm") ecs;}
@page Section1
{size:595.3pt 841.9pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
mso-header-margin:35.4pt;
mso-footer-margin:35.4pt;
mso-header:url("<%=Application("ServerPath")%>header.htm") h1;
mso-footer:url("<%=Application("ServerPath")%>header.htm") f1;
mso-paper-source:0;
mso-gutter-direction:rtl;}
div.Section1
{page:Section1;}
0
 
thotamaheshAuthor Commented:
Hi,

I have faced another problem. I have generated the word document using html and setting the content to convert to word document. but once the document is displayed on browser after clicking on 'Open' the 'Save As Type' is default set to 'Web Page' instead of 'Word Document. Also when the doc is saved and opened again using word then if we try to do 'Save As' then also 'Save As type' is default to 'Web page'....Is there any way can we make changes such 'Save As type' is set default to Word Document instead of Web page.


As I faced the above problem planning to generate the document using RTF Specification...is it correct method to do or any other simple method is present? Please suggest.
0
 
hujiCommented:
Well I need the relevant pieces of your code to see if I can help in that regards.
In continuation to my previous message, I didn't find a better solution yet. Using RTF was an option I considered about too, but I don't think it would support page numbers or would be easy enough to work with.
How are you going to use RTF if you need to have images inside your Word documents?
0
 
joeposter649Commented:
<<'Save As Type' is default set to 'Web Page' instead of 'Word Document.>>
Did you read about the filename here? http://www.aspfaq.com/show.asp?id=2129
0
 
thotamaheshAuthor Commented:
Following code currently I am using for generating word doc.

1. setting the content
2. creating the document  part
3. response.flush

ASP Part
--------------------------
Response.ContentType = "application/vnd.ms-word"
Response.AddHeader "content-disposition", "inline; filename="&projectNumber&".doc"

Document formatting part
--------------------
<table BORDER="1" align="center" width="100%" cellpadding=0 cellspacing=0 bordercolor=black style=solid>
      <tr>
            <td wrap  width=90% bgcolor="lightgrey" valign="middle" align="center" height=20px colspan=6>
            <font face="Arial" size=2> <h5>GPO Project Directory Monthly Report</h5></td>
      </tr>
      <tr>
            <td wrap  width=25% bgcolor="lightgrey" valign="middle" align="left" > <font face="Arial" size=1> <b>Project Name</b></font></td>
            <td wrap  width=45% bgcolor="white" valign="middle" align="left" colspan=3 > <font face="Arial" size=1> <%=projectName%> </font></td>
            <td wrap  width=15% bgcolor="lightgrey" valign="middle" align="left"> <font face="Arial" size=1><b>Report Month</b></font></td>
            <td wrap  width=15% bgcolor="white" valign="middle" align="left" ><font face="Arial" size=1>
            <%=month%></font></td>
      </tr>

      <tr>
            <td wrap  width=25% bgcolor="lightgrey" valign="middle" align="left" > <font face="Arial" size=1><b> Project Programme</b></font></td>
            <td wrap  width=15% bgcolor="white" valign="middle" align="left" > <font face="Arial" size=1> <%=projectProgramme%></font></td>
            <td wrap  width=15% bgcolor="lightgrey" valign="middle" align="left"> <font face="Arial" size=1><b>Business Owner</b></font></td>
            <td wrap  width=15% bgcolor="white" valign="middle" align="left" ><font face="Arial" size=1>
            <b><%=businessOwner%></b></font></td>
            <td wrap  width=15% bgcolor="lightgrey" valign="middle" align="left" > <font face="Arial" size=1> <b>Project Manager</b></font></td>
            <td wrap  width=15% bgcolor="white" valign="middle" align="left"  > <font face="Arial" size=1> <%=projectManager%></font></td>
      </tr>

      <tr>
            <td wrap  width=25% bgcolor="lightgrey" valign="middle" align="left" > <font face="Arial" size=1> <b>Planview ID</b></font></td>
            <td wrap  width=15% bgcolor="white" valign="middle" align="left" > <font face="Arial" size=1> <%=projectNumber%></font></td>
            <td wrap  width=15% bgcolor="lightgrey" valign="middle" align="left"> <font face="Arial" size=1><b>Technical Owner</b></td>
            <td wrap  width=15% bgcolor="white" valign="middle" align="left"  ><font face="Arial" size=1>
            <b><%=techOwner%></b></td>
            <td wrap  width=15% bgcolor="lightgrey" valign="middle" align="left" > <font face="Arial" size=1> <b>Project Phase</b></font></td>
            <td wrap  width=15% bgcolor="white" valign="middle" align="left"  > <font face="Arial" size=1> <%=phaseName%></font></td>
      </tr>
</table>
<br>
<table BORDER="1" align="center" width="100%" cellpadding=0 cellspacing=0 bordercolor=black style=solid>
      <tr>
            <td wrap  width=90% bgcolor="lightgrey" align="center"><font face="Arial"><h6>Status Report<h6></font></td>
      </tr>
      <tr>
            <td wrap  width=90% bgcolor="white"><font face="Arial" size=1><%=statusReport%> </font></td>
      </tr>
</table>
<br>
<table BORDER="1" align="center" width="100%" cellpadding=0 cellspacing=0 bordercolor=black style=solid>
      <tr><% =getRAGRow %></tr>

      <tr>
            <td wrap  width=15% align="left" > <font face="Arial" size=1> <%=overallRAGReason%></font></td>
            <td wrap  width=15% align="left" > <font face="Arial" size=1> <%=scheduleRAGReason%></font></td>
            <td wrap  width=15% align="left"> <font face="Arial" size=1><%=budgetRAGReason%></font></td>
            <td wrap  width=15% align="left" ><font face="Arial" size=1><%=resourcesRAGReason%></font></td>
            <td wrap  width=15% align="left" > <font face="Arial" size=1> <%=risksRAGReason%></font></td>
            <td wrap  width=15% align="left"  > <font face="Arial" size=1> <%=changesRAGReason%></font></td>
      </tr>
</table>
<br>


Asp part
--------
<%response.flush%>
<%response.end%>



0
 
thotamaheshAuthor Commented:
Joe,
I have gone through the URLs mentioned by you. Information about Filename is given in those URLs..and in my case file name is coming fine but the 'Save As Type' is set default to 'Web page'..and I need by default that is set to 'Word Document'. In both the below situations the save as type should be set to 'Word Document'.

once the document is displayed on browser after clicking on 'Open' then when i do File -> Save As -> 'Save As Type' is default set to 'Web Page' instead of 'Word Document.
Also when the doc is saved as Word and opened again using word then if we try to do 'Save As' in the word then also 'Save As type' is default to 'Web page'.

I have tried like this also....In the above code html part I have commented and put the below code but still the same behaviour.
Response.write("AAAAAAA") & Response.binarywrite("AAAAAAA")
-----------------------------------------------------------------
yes hugi...it is taking more effort to code using RTF specification and regarding Image I have not tried..stil I am in initial stages of changes....
----------------------------------------------------------------------
Joe,hugi - if you have any other way of achieving the above work please help me....

awaiting your replies...

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 8
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now