Solved

Create a Word document using ASP on Server.

Posted on 2006-06-11
17
1,176 Views
Last Modified: 2008-02-07
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
Comment
Question by:thotamahesh
  • 8
  • 6
  • 3
17 Comments
 
LVL 14

Assisted Solution

by:huji
huji earned 200 total points
ID: 16881592
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
 
LVL 14

Expert Comment

by:huji
ID: 16881654
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
 
LVL 14

Expert Comment

by:huji
ID: 16881663
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
 

Author Comment

by:thotamahesh
ID: 16883856
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
 

Author Comment

by:thotamahesh
ID: 16883989
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
 

Author Comment

by:thotamahesh
ID: 16884025
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
 

Author Comment

by:thotamahesh
ID: 16886644
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
 
LVL 14

Expert Comment

by:huji
ID: 16888339
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 15

Accepted Solution

by:
joeposter649 earned 50 total points
ID: 16888961
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
 
LVL 14

Expert Comment

by:huji
ID: 16895352
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
 
LVL 15

Expert Comment

by:joeposter649
ID: 16898215
<<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
 

Author Comment

by:thotamahesh
ID: 16901218
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
 

Author Comment

by:thotamahesh
ID: 16921734
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
 
LVL 14

Expert Comment

by:huji
ID: 16923667
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
 
LVL 15

Expert Comment

by:joeposter649
ID: 16924768
<<'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
 

Author Comment

by:thotamahesh
ID: 16926874
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
 

Author Comment

by:thotamahesh
ID: 16927232
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Leverage browser caching 2 46
Visual Project Bridge error 5 56
Classic ASP - x-editable save to SQL 3 64
Error viewing ASP page 12 97
I recently decide that I needed a way to make my pages scream on the net.   While searching around how I can accomplish this I stumbled across a great article that stated "minimize the server requests." I got to thinking, hey, I use more than one…
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

759 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

22 Experts available now in Live!

Get 1:1 Help Now