none
Inserting the contents of .DOC file into Word 2007 document RRS feed

  • Question

  • Hi everyone!

    I'm pretty new to Open XML so I'm having a little difficulty on this one:
    I'm trying to insert the contents of a Word 2003 document into a .DOCX file.
    Any idea on how to do it?

    I didn't have problems with .DOCX files as I used AltChunks.
    But AltChunks doesn't seem to work with .DOC files.

    Thanks in advance!
    Thursday, February 11, 2010 10:43 AM

Answers

  • I followed up with some people and was directed to the implementer notes for Open XML and Office: http://www.documentinteropinitiative.org/ECMA-376/reference.aspx

    Word 2007 supports the following content types for altChunk:

             application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml
             application/vnd.ms-word.document.macroEnabled.main+xml
             application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml
             application/vnd.ms-word.template.macroEnabledTemplate.main+xml
             message/rfc822
             application/xml
             application/rtf
             application/xhtml+xml
             text/html
             text/plain


    You will need to convert your doc file into another format before you can add the altChunk. You can leverage Word Automation Services to perform this conversion on the server with SharePoint 2010.


    Zeyad Rajabi (MS)
    Wednesday, February 17, 2010 10:06 PM

All replies

  • AltChunks should work with .doc files. You need to make sure you specify the correct content type (application/msword). Try that and let us know if that works for you.
    Zeyad Rajabi (MS)
    Friday, February 12, 2010 6:38 PM
  • Hi Zeyad Rajabi!

    Thanks for your reply!
    The following is my code: 

    private void button2_Click(object sender, EventArgs e)
    {
        string fileName = @"C:\word2007.docx";
        string docFile = @"C:\word2003.doc";
    
        try
        {
            this.Cursor = Cursors.WaitCursor;
    
            using (WordprocessingDocument wordProcessingDocument = WordprocessingDocument.Open(fileName, true))
            {
                Paragraph paragraph = wordProcessingDocument.MainDocumentPart.Document.Body.Descendants<Paragraph>().First();
    
                InsertWordDocument(wordProcessingDocument, paragraph, docFile);
    
                wordProcessingDocument.MainDocumentPart.Document.Save();
            }
        }
    
        finally
        {
            this.Cursor = Cursors.Default;
        }
    }
    
    private void InsertWordDocument(WordprocessingDocument wordprocessingDocument, Paragraph paragraphElement, string wordFile)
    {
        string fileExtension = Path.GetExtension(wordFile).ToLower();
        string altChunkId = "AltChunkId1";
        AlternativeFormatImportPart altChunkPart;
        AltChunk altChunk = new AltChunk();
    
        if (fileExtension != ".docx")
        {
            altChunkPart = wordprocessingDocument.MainDocumentPart.AddAlternativeFormatImportPart(
                "application/msword", altChunkId);
        }
    
        else
        {
            altChunkPart = wordprocessingDocument.MainDocumentPart.AddAlternativeFormatImportPart(
                AlternativeFormatImportPartType.WordprocessingML, altChunkId);
        }
    
        using (FileStream fileStream = new FileStream(wordFile, FileMode.Open))
        {
            altChunkPart.FeedData(fileStream);
        }
    
        altChunk.Id = altChunkId;
        OpenXmlElement topLevelParentElement;
    
        if (paragraphElement.Parent.LocalName == "body" || paragraphElement.Parent.LocalName == "tc")
        {
            topLevelParentElement = paragraphElement;
        }
    
        else
        {
            topLevelParentElement = paragraphElement.
                Ancestors().
                Where(element => (element.Parent != null) && (element.Parent.LocalName == "body" || element.Parent.LocalName == "tc")).
                First();
        }
    
        topLevelParentElement.InsertAfterSelf<AltChunk>(altChunk);
    }

    Everytime I open the output .DOCX file, I get the following error:
    "The file cannot be opened because there are problems with the contents."

    Did I miss something?

    Monday, February 15, 2010 3:08 AM
  • Thanks for sharing your code. You are adding the wrong type for the altChunk. Your code specifies: AlternativeFormatImportPartType.WordprocessingML

    Insteadm you should specify "application/msword". Try that and let us know if that works.
    Zeyad Rajabi (MS)
    Monday, February 15, 2010 8:34 PM
  • Hi again!

    My code must have been a bit confusing.
    The content type I used for .DOC files is "application/msword",
    and AlternativeFormatImportPartType.WordprocessingML for .DOCX files.

    After saving the document and opening it, I'm getting the "file cannot be opened" error.

    Tuesday, February 16, 2010 2:45 AM
  • I followed up with some people and was directed to the implementer notes for Open XML and Office: http://www.documentinteropinitiative.org/ECMA-376/reference.aspx

    Word 2007 supports the following content types for altChunk:

             application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml
             application/vnd.ms-word.document.macroEnabled.main+xml
             application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml
             application/vnd.ms-word.template.macroEnabledTemplate.main+xml
             message/rfc822
             application/xml
             application/rtf
             application/xhtml+xml
             text/html
             text/plain


    You will need to convert your doc file into another format before you can add the altChunk. You can leverage Word Automation Services to perform this conversion on the server with SharePoint 2010.


    Zeyad Rajabi (MS)
    Wednesday, February 17, 2010 10:06 PM

  • That's what I did for the mean time: convert the .DOC file to .DOCX using Word Automation.

    That was actually my original plan but I didn't want to create an instance of Word just to do the conversion,
    so I looked for a faster way to get around it.

    Anyway, thanks again!

    Thursday, February 18, 2010 6:46 AM