Populate Word template from ASP

G

GwenP

Hi

Does anyone know if there is a way to populate a Word template from an asp
page and then save it (to a directory on the IIS server) as a new document?

The template also resides on the IIS server.

I can hyperlink to the template but it always comes up read only which I
cannot amend anyway but ideally I'd like to poulate it with data from a form
on the asp page and save it as a new document all from the click of a button
without the user even needing to see any of it happening.

Many thanks
GwenP
 
B

Bob Barrows

GwenP said:
Hi

Does anyone know if there is a way to populate a Word template from
an asp page and then save it (to a directory on the IIS server) as a
new document?

The template also resides on the IIS server.

I can hyperlink to the template but it always comes up read only
which I cannot amend anyway but ideally I'd like to poulate it with
data from a form on the asp page and save it as a new document all
from the click of a button without the user even needing to see any
of it happening.
You will need a third-party component such as the one offerred by ASPOSE
(google it).

Yes, it is possible to automate Word, but it is not recommended:
http://support.microsoft.com/default.aspx?scid=KB;en-us;q257757
 
B

bmguk

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
 
D

Daniel Crichton

bmguk wrote on Wed, 6 May 2009 01:42:58 -0700 (PDT):
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.

This only applies to Word 2007, Word XP (2002) or 2003 (and older versions,
but I didn't test them) will just parse the XML as plain text.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top