dcsimg
 

ASP and Word

by ServerWatch Staff

Here is a new tutorial on how to write to a word document, change the contents of a word document and print it out.

by Armand Datema

Here is a new tutorial on how to write to a word document, change the contents of a word document and print it out. There will also be an update to this one and the excel one with more things you can do. Most of it comes from the the microsoft office VB help. But you have to do some fiddling with the code to get the VBscript code.

First we set the type of script

<%@ LANGUAGE="VBSCRIPT" %>

Make the object, and set the object to a word document

Dim MyDoc
Set MyDoc = CreateObject("Word.Document")
MyDoc.Application.Visible = True

Now lets write the rest of the script, see the comments

' Set the font properties
MyDoc.content.Font.Bold = True
MyDoc.content.Font.Italic = True
MyDoc.content.Font.Underline = True
MyDoc.content.Font.Name = "Comic Sans MS"
MyDoc.content.Font.Size = 25

' Insert some text
MyDoc.content.insertAfter "word1 word2 word3 word4"
MyDoc.content.InsertParagraphAfter

' Now lets change the colors and fontsize of the induvidual words that were just added, just look closely to the opened word document and you will see it change dynamically
MyDoc.Words(1).Font.Size = 10
MyDoc.Words(1).Font.ColorIndex = 1
MyDoc.Words(2).Font.Size = 20
MyDoc.Words(2).Font.ColorIndex = 2
MyDoc.Words(3).Font.Size = 30
MyDoc.Words(3).Font.ColorIndex = 3
MyDoc.Words(4).Font.Size = 40
MyDoc.Words(4).Font.ColorIndex = 4

' Print the document
MyDoc.PrintOut
' Release the object variable.
Set MyDoc = Nothing
%>

Now lets complete the HTML tags.

<HTML>
<HEAD>
<TITLE>Excelface</TITLE>
</HEAD>
<BODY>
</BODY>
</HTML>

This completes yer ASP page, look below for the complete code of mydoc.asp

<%@ LANGUAGE="VBSCRIPT" %>
<%
Dim MyDoc
Set MyDoc = CreateObject("Word.Document")
MyDoc.Application.Visible = True
MyDoc.content.Font.Bold = True
MyDoc.content.Font.Italic = True
MyDoc.content.Font.Underline = True
MyDoc.content.Font.Name = "Comic Sans MS"
MyDoc.content.Font.Size = 25
MyDoc.content.insertAfter "word1 word2 word3 word4"
MyDoc.content.InsertParagraphAfter
MyDoc.Words(1).Font.Size = 10
MyDoc.Words(1).Font.ColorIndex = 1
MyDoc.Words(2).Font.Size = 20
MyDoc.Words(2).Font.ColorIndex = 2
MyDoc.Words(3).Font.Size = 30
MyDoc.Words(3).Font.ColorIndex = 3
MyDoc.Words(4).Font.Size = 40
MyDoc.Words(4).Font.ColorIndex = 4
MyDoc.PrintOut
Set MyDoc = Nothing
%>
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
</BODY>
</HTML>

Download Code

Word and ASP

light.gif (1591 bytes)

Note: Here you will find extra information regarding the article. It could be additions, aprovements, links to other resources or other methods of resolving this problem. If you were looking for the code, go click the diskette to download it.


Back to the Article


This article was originally published on Thursday Jun 10th 1999
Home
Mobile Site | Full Site