R – Downloaded word file displaying incorrectly

asp.netms-wordvb.net

I am working on a website at the moment which is displaying a strange bug with generated word documents. The site has a feature on it which allows the user to download a word document containing information related to their visit. This file is generated via some vb.net code and takes an xml template of the final document and inserts the relevant content required.

The strange behaviour is that on some machines the .doc file generated displays fine and on others it displays as XML when opened in Word. Both behaviours have been seen in the same version of Office (2003) but on seperate machines. My question is really whether the error lies with the set up of word on the individual machines, or whether there is an error in the code.

The code to create the file and download it is as follows:

    Response.Clear()
    Response.ClearHeaders()
    Response.AddHeader("content-disposition", "inline; filename=MyNewFile")
    Response.ContentType = "application/msword"

    'Create the word file as a byte array based off an xml template document'
    Dim objWordGenerator As New WordFileGenerator
    Response.BinaryWrite(objWordGenerator.GetWordBytes)

    Response.Flush()
    Response.Clear()
    Response.End()

The actual xml template is quite large so probably not suitable to post here but I can provide any more information if necessary.

Update:
Having managed to fix the original bug (it turns out that the original filename being used didn't have the .doc extension) I have found another bit of strange behaviour.

When the file is opened it opens in Word correctly, however when you go to save it the default file type is XML. When saved as an XML file it will open in Word correctly, but I feel this is slightly confusing behaviour for the end user. I would like the file to default to saving as a DOC file instead. Is there a way to force this to happen?

Update 2:
Below is a section of the XML that relates to the Document properties. The rest of the document deals with content and styles etc, so my assumption is that this is the most relevant section. To reiterate, my problem is that when the downloaded .doc file is opened in word, the default "save as" option is as an XML file.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core" xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no" xml:space="preserve">
  <o:DocumentProperties>
    <o:Title>Fancy Word Doc</o:Title>
    <o:Author>Bob Bobertson</o:Author>
    <o:Characters>999</o:Characters>
    <o:Company>A Fancy Company</o:Company>
    <o:Version>1.1.1</o:Version>
  </o:DocumentProperties>

Cheers

Best Answer

The File -> SaveAs filetype is XML because that is what the file open in Word is. If you want it to say 'Word Document (*.doc) then you will need to create a real Word document on the server and not an XML. Just by putting a .doc extension on the filename doesn't change it's real contents. Word knows the file type that is loaded into it and suggests that as the file type when saving. I don't know of any way to override this behavior.

Related Topic