Link to home
Start Free TrialLog in
Avatar of LeanMoreTryMore
LeanMoreTryMore

asked on

Click the Save image to save the ASP page as a word document

We have a ASP page which collect all the information.
We have two image files:) Printer and Save

Where the Printer image file is clicked, it prints the ASP page to the printer, it works using the following statement

 <a href="javascript:print()"><img src="Image/tool-print.gif" width="20" height="20" border="0"></a>


My problme is I don't know how to do where the Save image is clicked, I want to save the ASP page as a word documnet.
Please advise.
Giving me some sample codes would be greatly appriciated.
Avatar of xiong8086
xiong8086

<%
Response.Clear
Response.ContentType = "application/msword"
Response.addHeader "content-disposition","attachment;filename=filename.doc"
%>

add this to the beginning of your ASP page. try again.

cheers,
xiong fei

to elaborate xiong8086's point...

Your link should be this:
<a href="<%=Request.ServerVariables("SCRIPT_NAME")%>?save"><img src="Image/tool-save.gif" width="20" height="20" border="0"></a>

Then on top of the page, you do this...
<%
If Request>QueryString="save" Then
    Response.Clear
    Response.ContentType = "application/msword"
    Response.addHeader "content-disposition","attachment;filename=filename.doc"
End If
%>
sorry... ii shd be
If Request.QueryString="save" Then
If you want to create a word document ground up (rather than rendering html into a doc), see this page...
http://www.freevbcode.com/ShowCode.asp?ID=1727
Avatar of LeanMoreTryMore

ASKER

Sorry fozylet. I dont quite understand

<a href="<%=Request.ServerVariables("SCRIPT_NAME")%>?save"><img src="Image/tool-save.gif" width="20" height="20" border="0"></a>

What does the  SCRIPT_NAME do? I this is a sever side variable.

Does that mean, when i click the save image, I need to call another page which add the following to the top of the page

<%
If Request>QueryString="save" Then
    Response.Clear
    Response.ContentType = "application/msword"
    Response.addHeader "content-disposition","attachment;filename=filename.doc"
End If
%>

I know it should be a silly question as I'm the beginner of the ASP page.
Well, lemme explain...

On whichever page to which you need to provide this 'save' button, add that code block right at the top.

What the SCRIPT_NAME does is to get the name of current file. So, wherever you put the save button, it'll take that page's name and do a call to the same page. this time the page will be rendered as a doc since querystring will have ?save in it now

hope that helps. don't hesitate to get back if am not making myself clear...

note that this method of printing won't work if you reached at this page by submitting a form or something... in that case u'll need to have the same form as hidden fields in this page and the save image shd be the submit button for that form
as a demo, just save the code below as an asp page...

<%
If Request.QueryString="save" Then
    Response.Clear
    Response.ContentType = "application/msword"
    Response.addHeader "content-disposition","attachment;filename=filename.doc"
End If
%>
<html><body>
<img src="http://photos1.blogger.com/img/163/3635/320/IMG_2675.jpg" >
<a href="<%=Request.ServerVariables("SCRIPT_NAME")%>?save"><img src="http://devel.akbkhome.com/xultest/save.gif" width="20" height="20" border="0"></a>
</body></html>
if (Request.QueryString=="save") {
    Response.Clear
      Response.Buffer = true
    Response.ContentType = "application/msword"
      Response.addHeader("content-disposition", "attachment;filename=myWord.doc");
}

I use the above statement, but I want to ask the user to enter the filename instead of using myWord.doc
even in this case user has an option of suggesting the file name (just that the default is myword.doc)
or u can remove this line... then the default file name will be current_asp_page.asp
    Response.addHeader "content-disposition","attachment;filename=filename.doc"

fozylet, thanks for the info.
another question. The asp page contains image files. if the user saves the asp file as a word document. I want that image attached the doucment as well instead of the document shown the cross sight

Please give me some idea. thanks
The image also will be saved in the word document in a normal scenario. If you are seeing a cross, word is somehow not able to access internet.
How can word be able to access internet? what else i need to tell the Web Administrator so that the word can be able to access internet.
Please advise
thank you in advance
ASKER CERTIFIED SOLUTION
Avatar of fozylet
fozylet
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks fozylet. You are very helpful.

I still have a question. Why the page goes straight to ask me to save the file? I expect it displays the information page and then the user clicks on the save image file and then it pops up the window and asks the user to save the file.

In  ReviewHRAcknowledgeForm.asp, this pages let the HR person to enter the comment. Once the Accept button is clicked, it
calls the ReviewHRAckForm_action.asp, this page I add the save image file. Lets say if the HR person clicks on the save image, it then allows him to save the asp page as a word document. (I PUT YOUR SUGGESTED CODES TO THIS PAGE)

SEE my codes below. and CORRECT ME IF I MADE MISTAKE
===============
<!-- static char rcsid[]="$Id:  $";
-->
<%@LANGUAGE="JSCRIPT" %>
<script> if(window.history.forward() != null) window.history.forward();</script>
<!--#include file="Connections/2000Connect.asp" -->
<%
Response.write("Request.QueryString:"+Request.QueryString)
if (Request.QueryString=="save") {
    Response.Clear
      Response.Buffer = true
    Response.ContentType = "application/msword"
      Response.addHeader("content-disposition", "attachment;filename=AppraisalForm.doc");
}
%>

<%
Response.ContentType = "application/vnd.ms-word"
var ipadd              = Request.servervariables("HTTP_REFERER")
var FEmpNo             = Request("employee_no")
var FreviewedEmpNo     = Request("reviewed_employee_no")
var FappraisalFromId   = Request("appraisal_form_id")
var FacceptFlag        = Request("accept_flag")
var FnextProcessStage  = Request("next_process_stage")
var Fcomments          = Request("your_comments")
var actionType

var Connect = MM_2000Connect_STRING
var Cnxn = Server.CreateObject("ADODB.Connection")
Cnxn.Open(Connect)

var AppraisalForm = Server.CreateObject("ADODB.Recordset")
var SQLAppraisalForm = "SELECT employee_no        " +
                          "      ,employee_name      " +
                                      "      ,emp_email_address  " +
                                      "      ,reviewer_no        " +
                                      "      ,reviewer_name      " +
                                      "      ,reviewer_email_address   " +
                                      "      ,NVL(location,'Unknown')   location   " +
                                      "      ,NVL(department,'Unknown') department " +
                                "      ,reviewer_year_service    " +
                                "      ,reviewer_month_service   " +
                                "      ,TO_CHAR(review_date,'DD/MM/YYYY')                   review_date   " +
                                "      ,NVL(TO_CHAR(review_from,'DD/MM/YYYY'),'IS_NULL')    review_from   " +
                                "           ,NVL(TO_CHAR(review_to,'DD/MM/YYYY'),'IS_NULL')      review_to     " +
                                    " FROM  em_web_appraisal_form     " +
                                    " WHERE appraisal_form_id   = TO_NUMBER(" + "'" + FappraisalFromId + "')"
AppraisalForm = Cnxn.Execute(SQLAppraisalForm)

var AppraisalFormDtl = Server.CreateObject("ADODB.Recordset")
var SQLAppraisalFormDtl = "SELECT * from em_web_appraisal_form_dtl " +
                          "WHERE   appraisal_form_id   = TO_NUMBER(" + "'" + FappraisalFromId + "')" +
                                       "AND  (  review_question    IS NOT NULL   " +
                          "OR     review_answer       IS NOT NULL   " +
                          "OR     selected_emp_rating IS NOT NULL ) " +
                          "ORDER BY heading_no, DECODE(rating_code,'NULL',1,2), DECODE(form_section,'H',1,2)"
AppraisalFormDtl = Cnxn.Execute(SQLAppraisalFormDtl)

var SQLstatm ="UPDATE em_web_appraisal_form     " +
                    "  SET comments                  =" + "'" + Fcomments   + "'" +                                                             
                    " WHERE appraisal_form_id        =TO_NUMBER(" + "'" +FappraisalFromId + "')"
Cnxn.Execute(SQLstatm)

var GetYourComments = Server.CreateObject("ADODB.Recordset")
var SQLGetYourComments = "SELECT comments FROM em_web_appraisal_form " +
                         " WHERE appraisal_form_id   =TO_NUMBER(" + "'" +FappraisalFromId + "')"
GetYourComments = Cnxn.Execute(SQLGetYourComments)                                    
var OverallComments = (GetYourComments("comments"))
                                     
                                     
var emailContent = ""
var actionType   = ""
if (FacceptFlag=="A") {
   var SQLstatm ="UPDATE em_review_signoff               " +
                       " SET   hr_acceptance  = 'Y'            " +
                         "      ,hr_accept_date = TRUNC(SYSDATE) " +
                         "      ,record_status      =  'C' "       +
                       " WHERE web_appraisal_form_id  = " + FappraisalFromId
   Cnxn.Execute(SQLstatm)
     
   var SQLstatm ="DELETE FROM em_queue   " +
                       "WHERE  reference_no =  " + "'" + FappraisalFromId + "'" +
                         " AND   process_type = 'V' "
   Cnxn.Execute(SQLstatm)
   
   actionType   = "A"  
}
else {
   actionType   = "N"
}
%>



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script language="JavaScript" src="Utilities.js"></script>
<title>Employee Management</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
<!--
body,td,th {
      font-family: Arial, Helvetica, sans-serif;
      font-size: 12px;
      color: #000000;
}
.style8 {font-size: 16px}
.style9 {font-size: 14px}
-->
</style>

</head>

<body marginwidth="0" marginheight="0" style="margin: 0" onLoad="writeMenus()" onResize="if (isNS4) nsResizeHandler()">
<form name="form1" method="post" action=""  >
<div align="left">
    <table width="100%"  border="0">
    <tr>
      <td width="80%" align="right" valign="top"> <img src="Image/img_crm.jpg" width="386" height="85.9">
    </tr>
   </table>

   <table width="100%"  border="0">
   <tr>
      <td align="left" valign="top">

<%
Server.Execute("TopSlideDownMenu.asp")
%>
<table width="100%"  border="1" bgcolor="#ECECEC">
    <th scope="col"><span class="style8">Performance Management System</span><BR>
      <span class="style9"><img src="Image/spacer.gif" width="1" height="1">Performance Appraisal Form</span>
       <a href="javascript:print()"><img src="Image/tool-print.gif" width="20" height="20" border="0"></a>
     <img src="Image/spacer.gif" width="10" height="1">
     <a href="<%=Request.ServerVariables("SCRIPT_NAME")%>?save"><img src="Image/save.jpeg" width="24" height="24" border="0"></a>
</table>

<table width="100%"  border="1">
  <tr>
    <td width="25%"><div align="right"><strong>Employee Code: </strong></div></td>
    <td width="75%" > <%= Response.Write(FreviewedEmpNo)%></td>
  </tr>
  <tr>
    <td><div align="right"><strong>Employee Name: </strong></div></td>
     <td width="75%" > <%= (AppraisalForm("employee_name")) %>        </td>
  </tr>
  <tr>
    <td><div align="right"><strong>Departmentt: </strong></div></td>
     <td width="75%" > <%= (AppraisalForm("department")) %></td>
  </tr>
  <tr>
    <td><div align="right"><strong>Location:</strong></div></td>
    <td width="75%" > <%= (AppraisalForm("location")) %></td>
  </tr>
  <tr>
    <td><div align="right"><strong>Reports To:</strong></div></td>
     <td width="75%" ><%= (AppraisalForm("reviewer_name")) %></td>
  </tr>
</table>

</P>
<table width="100%"  border="1" bgcolor="#ECECEC">
    <th scope="col"><span class="style8">Performance Appraisal</span>
</table>

<table width="100%"  border="1">
  <tr>
    <td width="25%"><div align="right"><strong>How long as employee's manager : </strong></div></td>
    <td width="75%" > <%= (AppraisalForm("reviewer_year_service")) %>
        (Years)
      <%= (AppraisalForm("reviewer_month_service")) %>
      (Months)
      </td>
  </tr>
 
  <tr>
    <td><div align="right"><strong>Appraisal date : </strong></div></td>
      
     <td width="75%" > <%= (AppraisalForm("review_date")) %>         </td>
  </tr>
 
  <tr>
    <td><div align="right"><strong>Appraisal From: </strong></div></td>
       <td width="75%"> <%= (AppraisalForm("review_from")) %>            </td>
  </tr>
 
  <tr>
    <td><div align="right"><strong>Appraisal To: </strong></div></td>
       <td width="75%"> <%= (AppraisalForm("review_to")) %>      </td>
  </tr>
</table>
</P>
<table width="100%" height="23"  border="1" bgcolor="#ECECEC">
<tr>
<% while (! AppraisalFormDtl.EOF) { %>      

   <% if ((AppraisalFormDtl("new_heading_flag"))=="Y")  { %>
 
        <table width="100%"  border="1" bgcolor="#ECECEC">
           <th scope="col" align="left"><span class="style8"><%= (AppraisalFormDtl("review_heading")) %></span>
        </table>            
             <table width="100%" border="1">
                <% if ((AppraisalFormDtl("rating_code"))!="NULL") { %>
                <table width="100%"  border="1">
                  <td width="43%" bgcolor="#F5AOBD"><div align="left" class="style16"><strong>Completencies </strong></div></td>
                  <td width="43%" bgcolor="#F5AOBD"><div align="left" class="style16 style15"><strong>Assessment</strong></div></td>
                  <td width="14%" bgcolor="#F5AOBD"><div align="left" class="style16 style15"><strong>Rating</strong></div></td>
                </table>                              
                  <% } %>
   
   <% } %>
        
   <% if ((AppraisalFormDtl("rating_code"))=="NULL") { %>
              <tr>
             <td width="25%"><strong><%= (AppraisalFormDtl("review_question")) %>:</strong></td>
               <td width="75%"><%= (AppraisalFormDtl("review_answer")) %> </td>
          </tr>

      <% } else {  %>
       <table width="100%" height="23"  border="1">
       <tr>
          <td valign="top" width="43%"><%= (AppraisalFormDtl("review_question")) %> </td>
          <td valign="top" width="43%"><%= (AppraisalFormDtl("review_answer")) %> </td>
          <td valign="top" width="43%"><%= (AppraisalFormDtl("selected_emp_rating")) %> </td>
       </tr>
       </table>
      <% }  %>

<%
  AppraisalFormDtl.MoveNext()
}
%>                        
</table>

<table width="100%"  border="1" bgcolor="#ECECEC">
    <th scope="col"><div align="left"><span class="style8">Your Comments</span>
    </table>

<table width="100%"  border="1">
  <tr>
  <td width="75%"><%= Response.write(OverallComments) %></td>
  </tr>
</table>      
<table width="100%"  border="1" bgcolor="#COCBE7">
  <tr>
    <td><table width="100%" height="34"  border="0" >
    </table>
    Tthe details shown in this form have been discussed between you and your reviewer. It does not necessarily indicate your agreement with the content of the appraisal. If you have any additional points to raise, please click on the I Do Not Accept button.</td>
  </tr>
</table>
</form>
</body>
</html>
<%
AppraisalForm.Close()
AppraisalForm = null
AppraisalFormDtl.Close()
AppraisalFormDtl = null
%>
<%
var RSallReviewStatus  = Server.CreateObject("ADODB.Recordset")
var SQLallReviewStatus = "SELECT EM_WEB_GET_ALL_REVIEW_STATUS"  + "('" + FappraisalFromId + "')" + " em_queue_id FROM DUAL " 
RSallReviewStatus = Cnxn.Execute(SQLallReviewStatus)      
var FemQueueId    = (RSallReviewStatus("em_queue_id"))
if (FemQueueId>0) {
   var SQLdeleteEmpJobQueue = "UPDATE em_queue SET job_status = 'C' WHERE em_queue_id = " + FemQueueId
   Cnxn.Execute(SQLdeleteEmpJobQueue)                        
}
%>

============================================================================
When the save image is clicked, it displays the File Download window and then I click on the save, it display
a window stating that

"Internet Explorer cannot download...ViewHRAckForm_action.asp?save from localhost."

I dont know why it comes up. Last time It pops up the window and allows me to enter the filename

Do i need to switch off something so that i can download the file? PLEASE ADVISE.
I have increase the point to 500.

Thanks in advance
It should be some settings issue on the user system... i can't think of anything
May be you could post another question in the MS-Word section

https://www.experts-exchange.com/Applications/MS_Office/Word/