Use MSWord document from an ASP page
hi
i have windows server with Microsoft Word 2007 now i want to create a MSWord document from an ASP page when Microsoft Word is installed on the server is that possible to do ? also tell me the way to open Microsoft Word Documents from .NET.
thank you
Re: Use MSWord document from an ASP page and .NET
Automation is a process that allows applications that are written in languages such as Visual Basic .NET or C# to programmatically control other applications. Automation to Word allows you to perform actions such as creating new documents, adding text to documents, mail merge, and formatting documents. With Word and other Microsoft Office applications, virtually all of the actions that you can perform manually through the user interface can also be performed programmatically by using automation. Word exposes this programmatic functionality through an object model. The object model is a collection of classes and methods that serve as counterparts to the logical components of Word. For example, there is an Application object, a Document object, and a Paragraph object, each of which contain the functionality of those components in Word.
try for step by stwp opration http://www.codeproject.com/KB/aspnet...plication.aspx
Re: Use MSWord document from an ASP page and .NET
This script creates a Microsoft Word document from an ASP page as long as Microsoft Word installed on the server. The content of the document is extracted from a MSAccess used to manage contacts / customers.
A first reading of the script allows us to make the following findings:
- You must have Word on the server. Failing the CreateObject method will generate an error.
- To access ASP WORD, Word must have been used on the server by the account used to access web. In Word, open the VBA editor, then close. This allows to write a file by default, type normal.dot. If this file is missing, you will get the following error: "Could not open macro storage".
- Creation of the Word object Set WordApp = CreateObject ( "Word.Application")
- Setting mode = false invisible WordApp.Application.Visible
- Creation of the document Set WordDoc = WordApp.Documents.Add ()
- For each paragraph:
Quote:
<%
'Addition of paragraph
Set newpar = WordDoc.Paragraphs.Add
set newrange = newpar.range
'Set the style of the paragraph
with newrange
. Bold = False
. Italic = false
. Font.Name = "Verdana"
. Font.Size = "12"
. ParagraphFormat.Alignment = 1
'Or using a house style:. Style = "Title 1"
end with
'Insert the contents of paragraph
newrange.insertBefore ( "My text in this paragraph" & vbcrlf)
'Or a page break: newrange.insertBreak
%>
Registration Document # WordDoc.SaveAs Server.MapPath ( "doc / temp.doc")
# Disconnect Word WordDoc.Close: WordApp.Quit
Re: Use MSWord document from an ASP page and .NET
Here now, the complete script:
Quote:
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"
%>