access2word.asp
<% 'Export to Word Access - Script Guillaume
'Connecting to the database.
'Remember to replace your db_path path + file
set db = Server.CreateObject ( "ADODB.Connection")
db.Open "DRIVER = (Microsoft Access Driver (*. mdb)); DBQ =" & db_path
set rs = Server.CreateObject ( "ADODB.Recordset")
rs.Open "SELECT * from Table", db, 1, 2
'Declaration of two new objects
Sunday WordApp, WordDoc
'Create the application object word
Set WordApp = CreateObject ( "Word.Application")
'No need to start Word on the server, it is not!
WordApp.Application.Visible = false
'Create the Word document
Set WordDoc = WordApp.Documents.Add ()
'Parcours du recordset. For each Item, creating several paragraphs containing
'The information in the database.
'Then jump page.
'Watch out, here is the VBA Word, which can sometimes be a little strange
'For instance, the operator = of VBA is not in ASP. Therefore replaced by
'Parentheses.
'Equally, the constants VBA kind WdAlignCenter are not known in ASP
'And returned 0. We must therefore use their value (0, 1, 2 ...)
Do while not rs.EOF
Set Mypar1 = WordDoc.Paragraphs.Add
set myRange1 = Mypar1.range
MyRange1.InsertBefore (rs.fields.item ( "Customer"). Vbcrlf & value)
MyRange1.Style = "Title 1"
MyRange1.ParagraphFormat.Alignment = 1
Set newpar = WordDoc.Paragraphs.Add
set newrange = newpar.range
with newrange
. Bold = False
. Italic = false
. Font.Name = "Verdana"
. Font.Size = "12"
end with
newrange.insertBefore ( "Phone Standard:" & rs.fields.item ( "tel_standard). vbcrlf & value)
newrange.insertBefore ( "Event:" & rs.fields.item ( "activity"). vbcrlf & value)
newrange.insertBefore (rs.fields.item ( "Communications"). vbcrlf & value)
newrange.insertBefore (rs.fields.item ( "CP"). vbcrlf & value)
newrange.insertBefore (rs.fields.item ( "Adresse3). vbcrlf & value)
newrange.insertBefore (rs.fields.item ( "Address2"). vbcrlf & value)
newrange.insertBefore (rs.fields.item ( "Address1"). vbcrlf & value)
Set Mypar2 = WordDoc.Paragraphs.Add
set MyRange2 = Mypar2.range
MyRange2.InsertBefore ( "Representative:" & rs.fields.item ( "First"). Value & "" & rs.fields.item ( "Name"). Value)
MyRange2.Font.Bold = true
set Myparag2 = WordDoc.Paragraphs.add
set Myrang2 = Myparag2.range
with Myrang2
. Bold = False
. Italic = True
. Font.Name = "Verdana"
. Font.Size = "10"
End With
myrang2.insertBefore ( "email:" & rs.fields.item ( "email"). vbcrlf & value)
myrang2.insertBefore ( "Mobile:" & rs.fields.item (mobile). vbcrlf & value)
myrang2.insertBefore ( "Fax:" & rs.fields.item ( "fax"). vbcrlf & value)
myRang2.insertBefore ( "Phone:" & rs.fields.item ( "phone"). vbcrlf & value)
Set Mypar3 = WordDoc.Paragraphs.Add
set MyRange3 = Mypar3.range
MyRange3.InsertBefore ( "Date of contact:" & rs.fields.item ( "Date"). Value)
MyRange3.Font.Bold = False
Set Mypar4 = WordDoc.Paragraphs.Add
set MyRange4 = Mypar4.range
MyRange4.InsertBefore ( "Comment:" & rs.fields.item ( "comment"). Value)
MyRange4.Font.Bold = False
rs.MoveNext
if not rs.EOF then
'Paragraph of page break as long as records
Set Mypar5 = WordDoc.Paragraphs.Add
set MyRange5 = Mypar5.range
MyRange5.InsertBreak
Set Mypar5 = WordDoc.Paragraphs.Add
set MyRange5 = Mypar5.range
end if
Loop
Registration of the document in word format
WordDoc.SaveAs Server.MapPath ( "doc / temp.doc")
'Close the document
WordDoc.Close
'Word and
WordApp.Quit
'And the recordset
rs.close
'Access and
db.Close
Set WordDoc = Nothing
Set WordApp = Nothing
Set rs = Nothing
Set db = Nothing
'Redirect the browser to the document to view, print and save it locally.
Response.Redirect "doc / temp.doc"
%>
Bookmarks