none
Sequence of records changed while generating document using Microsoft Word Template, content controls RRS feed

  • Question

  • I have created Microsoft Word Template Document. I have used Content controls, Macro to generate my final output document.

    I am reading data from XML file and replacing value in corresponding content controls. The XML contains List of Items and its different value fields. ie. <Items><Item1><Name></Name></Item1><Item2><Name></Name></Item2><Item3><Name></Name></Item3></Items>.

    The Content controls are having same Tag Name and are repeating. Macro parses xml, read document and replace value in content control based on Tag Name.

    The problem is when I run macro to generate output document, the sequence of Items got changed in word document.
    ie. Item2 appear first, then Item1 and Item3. When I debug my Macro code, it shows proper sequence in arraylist. However, the document shows data in different sequence.


    I want to keep sequence of Items same as it is in XML. Please advice, how can I achieve proper result in sequence?

    Best Regards.

    Monday, October 14, 2013 5:30 PM

Answers

  • Hi AKhil

    Thank you for the additional information and for confirming that it's content controls in tables and Shapes that are causing the behavior you're seeing.

    Shapes are processed in the order in which they're anchored in the text flow of the document. So even though you may see Shapes in a specific order it doesn't mean Word processes them in that order.

    There's an option in the "Display" tab of File/Options for "Object anchors". Make sure that's activated.

    Now, if you click on a Shape and "jiggle" it a bit with the mouse you should see a black anchor symbol somewhere in the left margin, probably next to the start of a paragraph. You can do this with each Shape to determine where it is anchored. You can also click an anchor and drag it to another paragraph.

    In order to be certain in which order Shapes will be processed, each would need to be anchored to a different paragraph, in the order in which you want them to be processed. This can be difficult if the page has few paragraphs, or many Shapes...


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, October 18, 2013 6:13 AM
    Moderator

All replies

  • Unless your code is a state secret, it will help if you show it.

    Greg Maxey Please visit my website at: http://gregmaxey.mvps.org/word_tips.htm

    Monday, October 14, 2013 5:52 PM
  • HI Akhil

    I concur with Greg, we need to see the code that extracts the data from the XML and writes it to the Content Controls.

    You say ""Content controls are having same Tag Name" and I suspect this may be where the problem lies. I take it you're using SelectContentControlByTag and then looping the array that returns? Or are you dynamically inserting the content controls as you pick up the content from the XML?

    Where are the content controls placed in the document? In a table?


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 15, 2013 1:55 PM
    Moderator
  • Thanks Greg and Cindy for your reply.

    I don't have permission to put any code outside. Sorry about that.

    Yes Cindy, I am using SelectContentControlByTag and then looping the array and applying text to tags. Content Controls are placed in the document. Some content controls are placed in table, while some are in shape.

    For Tables, I found the solution. I am taking position of the content control and sort them in an array by position and then fill each of them. This way I am able to fix sequence problem for content controls in table. However, this is not working for content controls in shapes.

    Is there any likewise solution for it?

    Thanks,

    Akhil Kinariwala

    Friday, October 18, 2013 1:59 AM
  • Hi AKhil

    Thank you for the additional information and for confirming that it's content controls in tables and Shapes that are causing the behavior you're seeing.

    Shapes are processed in the order in which they're anchored in the text flow of the document. So even though you may see Shapes in a specific order it doesn't mean Word processes them in that order.

    There's an option in the "Display" tab of File/Options for "Object anchors". Make sure that's activated.

    Now, if you click on a Shape and "jiggle" it a bit with the mouse you should see a black anchor symbol somewhere in the left margin, probably next to the start of a paragraph. You can do this with each Shape to determine where it is anchored. You can also click an anchor and drag it to another paragraph.

    In order to be certain in which order Shapes will be processed, each would need to be anchored to a different paragraph, in the order in which you want them to be processed. This can be difficult if the page has few paragraphs, or many Shapes...


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, October 18, 2013 6:13 AM
    Moderator