none
Able to open Word doc created but with errors

    Question

  • I am generating a Word doc using open XML. When I run the code on my own PC I don't get any errors, but when I deploy my code to a web server, users are getting two popup error messages. They are able to then view the document.

    "Word found unreadable content in xxx.docx. Do you want to recover the contents of this document? If you trust the source of this document, click Yes." (click yes and another error appears)

    "The file xxx.docx cannot be opened because there are problems with the contents." (clicking OK to this opens the document)

    The code I am using is based on a sample I found on the open XML website for doing a mail merge. I have an existing .dotx template, I open it, replace the mail merge tags with my own content, and save.

    I then stream that file to the browser like so:

     

    Response.ClearContent();

    Response.ClearHeaders();

    Response.ContentType =

    "application/vnd.openxmlformats-officedocument.wordprocessingml.document";

    Response.AddHeader(

    "Content-Disposition", "attachment;filename=\"coverletter.docx\"");

    Response.BinaryWrite(stream.GetBuffer());

    Response.Flush();

    Tuesday, July 02, 2013 2:02 PM

Answers

  • Hi all,

    Thanks for all your suggestions. I found a workaround for this issue - instead of trying to stream the Word doc via the browser, I am doing a Response.Redirect to the url for the generated file on the web server. This works like a charm.

    Vic


    Vic

    • Marked as answer by VicBB Wednesday, July 10, 2013 3:56 PM
    Wednesday, July 10, 2013 3:56 PM

All replies

  • I'm guessing the problem may because you're creating the document from a *.dotx, but are naming it *.docx. That's a different file type; the extension of an Office document must agree with its internal file type, or the document is considered to be "damaged".

    Try changing the document type (ChangeDocumentType method of the Open XML SDK Document object) to  WordprocessingDocumentType.Document.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, July 02, 2013 3:11 PM
    Moderator
  • Thank you Cindy. This is how it is handled in the code I downloaded from the open xml website:

    /// <summary>

    /// Converts the DOTX to DOCX

    /// </summary>

    /// <returns>True or False (with an exception) if successful in converting the document</returns>

    private RETURN_VAL ConvertTemplate() {

    try {

    MemoryStream msFile = null;

    using (Stream sTemplate = File.Open(_templateFilePath, FileMode.Open, FileAccess.Read)) {

    msFile =

    new MemoryStream((int)sTemplate.Length);

    sTemplate.CopyTo(msFile);

    msFile.Position = 0L;

    }

    using (WordprocessingDocument wpdFile = WordprocessingDocument.Open(msFile, true)) {

    wpdFile.ChangeDocumentType(DocumentFormat.OpenXml.

    WordprocessingDocumentType.Document);

    MainDocumentPart docPart = wpdFile.MainDocumentPart;

    docPart.AddExternalRelationship(

    "http://schemas.openxmlformats.org/officeDocument/2006/relationships/attachedTemplate", new Uri(_templateFilePath, UriKind.RelativeOrAbsolute));

    docPart.Document.Save();

    }

    // Flush the MemoryStream to the file

    File.WriteAllBytes(_targetFileName, msFile.ToArray());

    msFile.Close();

    return new RETURN_VAL { Value = true };

    }

    catch (Exception ex) {

    return new RETURN_VAL { Value = false, Exception = "DocumentGeneration::convertTemplate() - " + ex.ToString() };

    }

    }


    Vic

    Tuesday, July 02, 2013 3:33 PM
  • Hi Vic

    The code you show looks like it ought to work. In that case, there's no way to tell what the problem may be in the documents you're generating. You should get a copy of the original file, as you send it and a copy of such a file that's been repaired. Use the Open XML SDK Productivity Tool to compare the two and see what the differences are. That may help you pinpoint what the problem is.

    Are you using the same version of Office as the end users when you open a document and say "on my own PC I don't get any errors"?


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, July 02, 2013 4:01 PM
    Moderator
  • I am using the same version of Office. In fact I do get the errors when I run my code on the web server, but not from localhost.

    How do I repair the Word doc I generated?


    Vic

    Tuesday, July 02, 2013 4:58 PM
  • That's interesting, and not something I'd expect with the Open XML SDK. There's no way to "repair" the result, short of opening it in Word. You need to do some detective work, as I described in a previous reply, and track down what the problem is / what could be causing it.

    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, July 02, 2013 5:02 PM
    Moderator
  • Hello Vic,

    I would suggest to open the resultant file using Open XML SDK tool and choose Validate to check if the Open XML generated is valid. If it shows the errors, you can investigate on the specific code which generated the Open XML.

    Thanks,

    Sreerenj G Nair

    Monday, July 08, 2013 2:36 PM
  • Hi Sreerenj,

    I did check that already, and the generated doc is valid.

    Thanks,

    Vic


    Vic

    Monday, July 08, 2013 4:34 PM
  • Hello Vic,

    As Cindy mentioned, you should compare the files generated from localhost and the webserver. Check what is the difference between both. This should proabably give an idea on where it is failing.

    Thanks,

    Sreerenj G Nair

    Tuesday, July 09, 2013 5:41 PM
  • Hi all,

    Thanks for all your suggestions. I found a workaround for this issue - instead of trying to stream the Word doc via the browser, I am doing a Response.Redirect to the url for the generated file on the web server. This works like a charm.

    Vic


    Vic

    • Marked as answer by VicBB Wednesday, July 10, 2013 3:56 PM
    Wednesday, July 10, 2013 3:56 PM