ASP and Word

By ServerWatch Staff (Send Email)
Posted Jun 10, 1999


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.

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

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

Page 1 of 2


Comment and Contribute

Your name/nickname

Your email

(Maximum characters: 1200). You have characters left.