General discussion

Locked

Edit Word Doc On Web Before Download

By John Gordon ·
I have a web site which has a Word document for users to download. Before they download I would like to edit the document to insert the username (they have logged in to the web site so I know who they are), and a unique file ID number that I have generated, in the footer of the document.

I can already edit the document locally on my office PC from an Access database using Visual Basic for Applications but how do I set this up to happen automatically on my web server?

Any help/suggestions would be appreciated.

This conversation is currently closed to new comments.

3 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

by Jaqui In reply to Edit Word Doc On Web Befo ...

in the vbscript the data needing to be inserted is variables, assigned by the server when it calls the script as cgi to download the document.

Collapse -

by AaronH In reply to Edit Word Doc On Web Befo ...

Well, you didn?t specify what language you wanted an answer in but here is on in Asp.Net (VB.Net) that accomplishes it without anything too fancy and can be adapted to any language.

1. Create the document that you want to inject the users name into.
2. Save it as Xml from Word. (documentTemplate.xml)
3. Edit the Xml file and put some kind of a token where you want to place the user name. In this case we are using 6 X?s, so XXXXXX. See the Xml snippet below.
4. Put the Xml version of the document on your website.
5. Each time you need to serve it to a user, you programatically:
a. Open the xml and read it into a temp variable.
b. Replace the token (XXXXXX) with the user name.
c. Write the temp variable back out to the file system.
d. Redirect the user to the file.


------This is part of the saved Word Xml. You can see where the token is added.
<w:body><wx:sect><w><w:r><w:t>Hello my name is XXXXXX.</w:t></w:r></w:body>

--------Code to do this in vb.net and Asp.Net. It?s taken for granted in this example that all files are located in the web root directory and all permissions, etc have been granted. An important update to this code would be to make the output file name unique for each user so that you don?t have a race condition.

Dim reader As StreamReader
Dim writer As StreamWriter
Try

Dim sourcePath As String = Path.Combine(Server.MapPath("."), " documentTemplate.xml")
reader = New StreamReader(sourcePath)

Dim body As String = reader.ReadToEnd.Replace("XXXXXX", "New User Name")

Dim destinationPath As String = Path.Combine(Server.MapPath("."), "NewFileForUser.doc")

writer = New StreamWriter(destinationPath)
writer.Write(body)

Response.Redirect("NewFileForUser.doc")

Catch ex As Exception
debug.writeline(ex.Message)
Throw
Finally
If Not reader Is Nothing Then reader.Close()
If Not writer Is Nothing Then writer.Close()
End Try

Collapse -

by AaronH In reply to

Well, the smiley face doesn?t belong in the Xml snippet, but there isn?t a preview to check these things before submitting.

Back to Web Development Forum
3 total posts (Page 1 of 1)  

Related Discussions

Related Forums