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


<% OptionĀ  Explicit %>
<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<%
Const wdAlignParagraphCenter = 1
Const wdAlignParagraphRight = 2

Dim Wrd,WrdDoc,DocName,objTable
Dim Conn,strSQL,objRec,arrCus,intRows
Dim MyRange1,MyRange2,MyRange3
Set Wrd = CreateObject("Word.Application")
DocName = "MyDoc/MyWord.doc"
Wrd.Application.Visible = False

Set WrdDoc = Wrd.Documents.Open(Server.MapPath("shotdev.dot"))

Set MyRange1 = WrdDoc.Paragraphs.Add.Range
With MyRange1
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Font.Name = "Verdana"
.Font.Size = "20"
.Font.Bold = True
.InsertBefore("Customer Report"&vbCrLf)
End With

Set Conn = Server.Createobject("ADODB.Connection")
Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("MyDoc/mydatabase.mdb"),"" , ""
strSQL = "SELECT * FROM customer "
Set objRec = Server.CreateObject("ADODB.Recordset")
objRec.Open strSQL, Conn, 1,3

If Not objRec.EOF and Not objRec.BOF Then
arrCus = objRec.GetRows()
End If

Set MyRange2 = WrdDoc.Paragraphs.Add.Range
With MyRange2
.Font.Size = "10"
End With
Set objTable = Wrd.ActiveDocument.Tables.Add(MyRange2,Ubound(arrCus),6,1,2) '** Range,Rows,Column **'

'*** Header ***'
objTable.Cell(1,1).Range.InsertAfter("CustomerID")
objTable.Cell(1,1).Range.Bold = True
objTable.Cell(1,1).Range.ParagraphFormat.Alignment = 1

objTable.Cell(1,2).Range.InsertAfter("Name")
objTable.Cell(1,2).Range.Bold = True
objTable.Cell(1,2).Range.ParagraphFormat.Alignment = 1

objTable.Cell(1,3).Range.InsertAfter("Email")
objTable.Cell(1,3).Range.Bold = True
objTable.Cell(1,3).Range.ParagraphFormat.Alignment = 1

objTable.Cell(1,4).Range.InsertAfter("CountryCode")
objTable.Cell(1,4).Range.Bold = True
objTable.Cell(1,4).Range.ParagraphFormat.Alignment = 1

objTable.Cell(1,5).Range.InsertAfter("Budget")
objTable.Cell(1,5).Range.Bold = True
objTable.Cell(1,5).Range.ParagraphFormat.Alignment = 1

objTable.Cell(1,6).Range.InsertAfter("Used")
objTable.Cell(1,6).Range.Bold = True
objTable.Cell(1,6).Range.ParagraphFormat.Alignment = 1

'*** Detail ***
For intRows = 0 To Ubound(arrCus,2)
objTable.Cell(intRows+2,1).Range.InsertAfter(arrCus(0,intRows))
objTable.Cell(intRows+2,1).Range.ParagraphFormat.Alignment = 1

objTable.Cell(intRows+2,2).Range.InsertAfter(arrCus(1,intRows))
objTable.Cell(intRows+2,2).Range.ParagraphFormat.Alignment = 0

objTable.Cell(intRows+2,3).Range.InsertAfter(arrCus(2,intRows))
objTable.Cell(intRows+2,3).Range.ParagraphFormat.Alignment = 0

objTable.Cell(intRows+2,4).Range.InsertAfter(arrCus(3,intRows))
objTable.Cell(intRows+2,4).Range.ParagraphFormat.Alignment = 1

objTable.Cell(intRows+2,5).Range.InsertAfter(FormatNumber(arrCus(4,intRows),2))
objTable.Cell(intRows+2,5).Range.ParagraphFormat.Alignment = 2

objTable.Cell(intRows+2,6).Range.InsertAfter(FormatNumber(arrCus(5,intRows),2))
objTable.Cell(intRows+2,6).Range.ParagraphFormat.Alignment = 2
Next

Set MyRange3 = WrdDoc.Paragraphs.Add.Range
With MyRange3
.ParagraphFormat.Alignment = wdAlignParagraphRight
.Font.Name = "Verdana"
.Font.Size = "10"
.InsertBefore(vbCrLf&vbCrLf&vbCrLf&"............................Manager"&vbCrLf&Now())
End With

WrdDoc.SaveAs(Server.MapPath(DocName))
Wrd.Application.Quit
Set Wrd = Nothing
%>

Word Created <a href="<%=DocName%>">Click here</a> to Download.
</body>
</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.