Hi Gwen
You can do this with a little preparation of the word document
beforehand and then using a Replace() function, I have a link
somewhere to online instructions but for the life of me I cannot find
it so I will try and explain best I can.
To prepare your template document:
Create a word document to use as you template and the items that you
want to replace you need to give them distinctive names as these are
going to be replaced later. For example if you have a word doc which
will have the first name and last name filled in automatically then
name these items FIRST_NAME & LAST_NAME respectively (Note: I use
underscores for spaces as this tends to give a more distinctive name
to the replaceable items)
When you have distinctively named your replaceable items save the
document as an XML file (File > Save As > MS Word XML File)
Now change the file extension to a .TXT. This stops the server reading
the document into memory later as an XML file and forces it in as
plain text. You can also give the file a more distinctive name but I
would advise that you have no spaces in the filename. For this
explanation we will call the file WORDTEMP.TXT.
You now need to open the file in a text editor (Notepad is fine) and
remove the carridge return at the end of the first 2 lines in the file
then save the file (still as a text file mind). This is needed because
otherwise when you read the file into memory later it will only read
the first line of the file. Wierd I know but it took me a few hours to
work this out when I first tried this method.
That is your word template prepared, so store that on your web server
in a fixed location so that you can open it as required. e.g. /
docstore
To Build The Word Document
Use the FileSystemObject to open the file
'First build the path name to the template file
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
theFile = "WORDTEMP.TXT"
thePathName = "/docstore/" & theFile
If FSO.FileExists(Server.MapPath(thePathName)) Then 'File exists
'Open the the template file
Set templatefile = FSO.OpenTextFile(Server.MapPath(thePathName))
'Load the template file into a variable called theinfo
theinfo = templatefile.ReadAll
'Close the file
templatefile.Close
'Now replace the Keywords with your dynamic content, I also always
replace ampersands with and as they can cause problems when saving the
file
theinfo = Replace(theinfo, "FIRST_NAME", Replace(Request.Form
("FirstName"), "&", "and"))
theinfo = Replace(theinfo, "LAST_NAME", Replace(Request.Form
("LastName"), "&", "and"))
'Create a new word document file
worddocname = "NEWWORDFILE.DOC"
thePathName = "/docstore" & "/" & worddocname
Set templatefile = FSO.CreateTextFile(Server.MapPath(thePathName),
true)
'Write the new info to the new file
templatefile.Write(theinfo)
'Close the new file
templatefile.Close
Set FSO = Nothing
End If
You will now have a word document available to utilise as you wish.
Don't be concerned that your document is an XML file as Word is more
than happy to open it and parse it correctly.
Points to note:
I use this method on Intranet applications mainly, if I am using it on
a public domain then I always try to do my document storage outside of
my web root.
You will probably need Modify permissions on the folders that you read/
write your documents to, the only way to test if you have these
permissions is to either ask your web hosting company or just try it.
If the documents don't appear or you get a permission denied error
then you will have to talk nicely to your hosting provider/system
administrator. I have never had a problem before getting permissions
set so I would guess niether would you.
Hope this helps.
Bren