Welcome to OpenXML Developer Sign in | Join | Help

Word 2007 cfChunk/altChunk

Last post 10-23-2008, 3:01 PM by Anand.Iyer. 6 replies.
Sort Posts: Previous Next
  •  09-26-2008, 2:45 PM 3739

    Word 2007 cfChunk/altChunk

    I guess I have 2 questions:

    1. In thedocumentation on msdn referring to Openxml and Word 2007, I noticed that there was a schema for the relationship cfChunk. Does that mean that I can use the 2003 cfChunk in Office 2007?

    2. I have tried to use altChunk to add an rtf file into my .docx by manipulating the xml file I added the necessary relationship, the content type, and the code in document.xml, but the file opens as corrupt. Does altChunk not work in Microsoft Office Enterprise 2007?

    I really need a way to add a .docx into another .docx file and I have tried just about everything I have found in the forums.

  •  09-30-2008, 5:48 PM 3749 in reply to 3739

    Re: Word 2007 cfChunk/altChunk

    It is pretty easy to insert one docx inside another using Open Xml SDK v2 .  Here is roughly what I do to insert a docx file "indocument" right infront content control sdtBlock:

    AlternativeFormatImportPart inDocPart = doc.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML);

    //Open file to be inserted and stream its contents into the part
    using (FileStream stream = new FileStream(indocument, FileMode.Open))
    {
          inDocPart.FeedData(stream);
    }
    //Create a AltChunk - <w:altChunk> and give the reltionship if linking it to the
    //alternative format part we added above
    AltChunk altChunk = new AltChunk();
    altChunk.Id = doc.MainDocumentPart.GetIdOfPart(inDocPart);
    //Insert the altChunk tag just before content control
    sdtBlock.InsertBeforeSelf(altChunk);
  •  10-01-2008, 11:31 AM 3750 in reply to 3749

    Re: Word 2007 cfChunk/altChunk

    Thank you so much I will try it. Do you know if there is any good documentation for this?
  •  10-06-2008, 5:34 PM 3757 in reply to 3750

    Re: Word 2007 cfChunk/altChunk

    Hi thanks for the code snippet for the OpenXml sdk v2.0

    I have an unanswered question I was wondering at the bottom of the code

    where you have :

    //Insert the altChunk tag just before content control
    sdtBlock.InsertBeforeSelf(altChunk);

    How can I just insert the altChunk at the bottom of the .docx or what is sdtBlock?

  •  10-07-2008, 12:35 PM 3769 in reply to 3757

    Re: Word 2007 cfChunk/altChunk

    vmoss:

    Hi thanks for the code snippet for the OpenXml sdk v2.0

    I have an unanswered question I was wondering at the bottom of the code

    where you have :

    //Insert the altChunk tag just before content control
    sdtBlock.InsertBeforeSelf(altChunk);

    How can I just insert the altChunk at the bottom of the .docx or what is sdtBlock?



    Here is how you can insert a file at the end of the document (after the last paragraph):

                string document = @"D:\TestDoc.docx";
                string indocument = @"D:\InDoc.docx";
                using (WordprocessingDocument doc = WordprocessingDocument.Open(document, true))
                {
                    //Get the last paragraph in the document
                    Paragraph paragraph = doc.MainDocumentPart.Document.Descendants<Paragraph>().Last();
                    //Create a new Alternative Format part
                    AlternativeFormatImportPart inDocPart = doc.MainDocumentPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML);

                    //Open file to be inserted and stream its contents into the part
                    using (FileStream stream = new FileStream(indocument, FileMode.Open))
                    {
                        inDocPart.FeedData(stream);
                    }
                    //Create a AltChunk - <w:altChunk> and give the reltionship if linking it to the
                    //alternative format part we added above
                    AltChunk altChunk = new AltChunk();
                    altChunk.Id = doc.MainDocumentPart.GetIdOfPart(inDocPart);
                    //Insert the altChunk tag after the paragraph
                    paragraph.InsertAfterSelf(altChunk);

                    //Save the document
                    doc.MainDocumentPart.Document.Save();
                }

  •  10-07-2008, 2:01 PM 3770 in reply to 3769

    Re: Word 2007 cfChunk/altChunk

    IT WORKED!!!!!!!!!!! That's one headache out of the window!

    Thank you very much vj9999.

  •  10-23-2008, 3:01 PM 3821 in reply to 3769

    Re: Word 2007 cfChunk/altChunk

    Normal 0 false false false MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0in 5.4pt 0in 5.4pt; mso-para-margin:0in; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} Hi,


    Thanks for the code snippet. It really helped out a lot. There is one other issue involved here.

    Some parts of the merged(second) document are reformatted using the style of the first document, particularly the bulleting & numbering. I tried to fix this using quite a few options. Couple of these are mentioned below,

    Snippet 1 -
    DocumentFormat.OpenXml.Wordprocessing.Locked locked1 = doc.MainDocumentPart.StyleDefinitionsPart.Styles.Descendants<Locked>().Last();
    locked1.Val = BooleanValues.False;
    doc.MainDocumentPart.Document.Save();


    Snippet 2 -
    AltChunkProperties properties = new AltChunkProperties();
    altChunk.AltChunkProperties = properties;

    MatchSource matchSource = new MatchSource();
    matchSource.Val = BooleanValues.True;
    altChunk.AltChunkProperties.MatchSource = matchSource;


    None of these options work.

    Are there any other properties which need to be set apart from the ones mentioned above?

    Any help on this is highly appreciated.

    Thanks,
    Anand.

View as RSS news feed in XML