I've been working on a project where a user can naviagate to a web page, and choose to download the latest information on a project as a word document. This information can contain grids, attachments, all sorts of things. I decided to take the easy/quick way of doing this, and just write out HTML to the actual web page. On the response object, i would simply set:
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.Charset = ""
HttpContext.Current.Response.ContentType = "application/msword"
HttpContext.Current.Response.AddHeader("Content-Disposition", "inlinefilename=" + strFileName)
i would then write out the content as HTML, where strHTMLContent is a stringbuilder. Something like:
strHTMLContent.Append("<br/>")
strHTMLContent.Append("<table align="'Center'" width="'100%'">")
strHTMLContent.AppendLine("<tr><th>Attachments:</th></tr>")
strHTMLContent.AppendLine("<tr><td>")
'blah blah blah, and then i would finish it by:
HttpContext.Current.Response.Write(strHTMLContent)
HttpContext.Current.Response.End()
HttpContext.Current.Response.Flush()
This works fine for the most part, but the attachments make it a bit annoying. For images, i just use a reference to "<img src='viewattachment.aspx?...",, where i created a secondary web page called "viewattachment" that can display my attachments based on a primary key value.
When the word doc gets saved, since it has the .doc extension, it's opened by Word, but it isn't an actual word file, regardless of the ContentType value I entered in at the top. This means that opening the word file calls the link to the viewattachment.aspx web page, which isn't really acceptable if the client opening the word doc has no internet connection. If you choose to 'save as' for the document, it will show that it sees it as an .html file. If you choose to save it as a word document, then that embeds the image.
An annoying extra step, but probably what I have to deal with by taking the easy way out and avoiding word COM objects.
The last point was that using the "<iframe src=''" string to show attachments doesn't work at all. It just doesn't show up in the word document, HTML version or DOC version. Some of our attachments are html documents, and i was hoping to display them that way, but it looks like i'll have to open each document one-by-one and write out their text.
Labels: document, export, report, word