web 2.0

How to use ASP & Word (Word.Application) - Word Document Sample Report

How to use ASP & Word (Word.Application) - Word Document Sample Report This is learn/tutorial asp developers how to using ASP script Export/Report to word document

ShotDev Focus:
- ASP & Export/Report to word document

Example

asp_word_report.asp

  1. <% Option  Explicit %>  
  2. <html>  
  3. <head>  
  4. <title>ShotDev.Com Tutorial</title>  
  5. </head>  
  6. <body>  
  7. <%  
  8. Const wdAlignParagraphCenter = 1  
  9. Const wdAlignParagraphRight = 2  
  10.   
  11. Dim Wrd,WrdDoc,DocName,objTable  
  12. Dim Conn,strSQL,objRec,arrCus,intRows  
  13. Dim MyRange1,MyRange2,MyRange3  
  14. Set Wrd = CreateObject("Word.Application")  
  15. DocName = "MyDoc/MyWord.doc"  
  16. Wrd.Application.Visible = False  
  17.   
  18. Set WrdDoc = Wrd.Documents.Open(Server.MapPath("shotdev.dot"))  
  19.   
  20. Set MyRange1 = WrdDoc.Paragraphs.Add.Range  
  21. With MyRange1  
  22. .ParagraphFormat.Alignment = wdAlignParagraphCenter  
  23. .Font.Name = "Verdana"  
  24. .Font.Size = "20"  
  25. .Font.Bold = True  
  26. .InsertBefore("Customer Report"&vbCrLf)  
  27. End With  
  28.   
  29. Set Conn = Server.Createobject("ADODB.Connection")  
  30. Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("MyDoc/mydatabase.mdb"),"" , ""  
  31. strSQL = "SELECT * FROM customer "  
  32. Set objRec = Server.CreateObject("ADODB.Recordset")  
  33. objRec.Open strSQL, Conn, 1,3  
  34.   
  35. If Not objRec.EOF and Not objRec.BOF Then  
  36. arrCus = objRec.GetRows()  
  37. End If  
  38.   
  39. Set MyRange2 = WrdDoc.Paragraphs.Add.Range  
  40. With MyRange2  
  41. .Font.Size = "10"  
  42. End With  
  43. Set objTable = Wrd.ActiveDocument.Tables.Add(MyRange2,Ubound(arrCus),6,1,2) '** Range,Rows,Column **'  
  44.   
  45. '*** Header ***'  
  46. objTable.Cell(1,1).Range.InsertAfter("CustomerID")  
  47. objTable.Cell(1,1).Range.Bold = True  
  48. objTable.Cell(1,1).Range.ParagraphFormat.Alignment = 1  
  49.   
  50. objTable.Cell(1,2).Range.InsertAfter("Name")  
  51. objTable.Cell(1,2).Range.Bold = True  
  52. objTable.Cell(1,2).Range.ParagraphFormat.Alignment = 1  
  53.   
  54. objTable.Cell(1,3).Range.InsertAfter("Email")  
  55. objTable.Cell(1,3).Range.Bold = True  
  56. objTable.Cell(1,3).Range.ParagraphFormat.Alignment = 1  
  57.   
  58. objTable.Cell(1,4).Range.InsertAfter("CountryCode")  
  59. objTable.Cell(1,4).Range.Bold = True  
  60. objTable.Cell(1,4).Range.ParagraphFormat.Alignment = 1  
  61.   
  62. objTable.Cell(1,5).Range.InsertAfter("Budget")  
  63. objTable.Cell(1,5).Range.Bold = True  
  64. objTable.Cell(1,5).Range.ParagraphFormat.Alignment = 1  
  65.   
  66. objTable.Cell(1,6).Range.InsertAfter("Used")  
  67. objTable.Cell(1,6).Range.Bold = True  
  68. objTable.Cell(1,6).Range.ParagraphFormat.Alignment = 1  
  69.   
  70. '*** Detail ***  
  71. For intRows = 0 To Ubound(arrCus,2)  
  72. objTable.Cell(intRows+2,1).Range.InsertAfter(arrCus(0,intRows))  
  73. objTable.Cell(intRows+2,1).Range.ParagraphFormat.Alignment = 1  
  74.   
  75. objTable.Cell(intRows+2,2).Range.InsertAfter(arrCus(1,intRows))  
  76. objTable.Cell(intRows+2,2).Range.ParagraphFormat.Alignment = 0  
  77.   
  78. objTable.Cell(intRows+2,3).Range.InsertAfter(arrCus(2,intRows))  
  79. objTable.Cell(intRows+2,3).Range.ParagraphFormat.Alignment = 0  
  80.   
  81. objTable.Cell(intRows+2,4).Range.InsertAfter(arrCus(3,intRows))  
  82. objTable.Cell(intRows+2,4).Range.ParagraphFormat.Alignment = 1  
  83.   
  84. objTable.Cell(intRows+2,5).Range.InsertAfter(FormatNumber(arrCus(4,intRows),2))  
  85. objTable.Cell(intRows+2,5).Range.ParagraphFormat.Alignment = 2  
  86.   
  87. objTable.Cell(intRows+2,6).Range.InsertAfter(FormatNumber(arrCus(5,intRows),2))  
  88. objTable.Cell(intRows+2,6).Range.ParagraphFormat.Alignment = 2  
  89. Next  
  90.   
  91. Set MyRange3 = WrdDoc.Paragraphs.Add.Range  
  92. With MyRange3  
  93. .ParagraphFormat.Alignment = wdAlignParagraphRight  
  94. .Font.Name = "Verdana"  
  95. .Font.Size = "10"  
  96. .InsertBefore(vbCrLf&vbCrLf&vbCrLf&"............................Manager"&vbCrLf&Now())  
  97. End With  
  98.   
  99. WrdDoc.SaveAs(Server.MapPath(DocName))  
  100. Wrd.Application.Quit  
  101. Set Wrd = Nothing  
  102. %>  
  103.   
  104. Word Created <a href="<%=DocName%>">Click here</a> to Download.  
  105. </body>  
  106. </html>  

Create a asp file and save to path root-path/myasp/

Run
http://localhost/asp_word_report.asp

Screenshot

ASP & Export/Report to word document.
.
.
.
Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (1 votes, average: 1.00 out of 10)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.