none
Range.InsertXML, Tables, and paragraph indentation RRS feed

  • Question

  • Hi

    I'm working on a project that involves dynamic generation of a Word 2010 (xlsx) document. It is based on a template we've created containing content controls into which text is being inserted from our VSTO add-in.

    The particular issue I'm attempting to resolve relates to the way content generated in the RichTextContentControls is not respecting indentation set  in the template. The indentation is set to 1.5cm on the left, applied to the whole template document, so the (presently empty) content controls are also indented by this amount. I'd like the content of these content controls to be indented by at least this amount once the document generation is complete. My issues are two-fold:

    1. Using Range.InsertXML does not appear to respect the paragraph indentation set in the content control, rather it simply inserts the XML document content with whatever indentation values that content already has, usually none. I've tried manually overriding the indentation value, after the call to Range.InsertXML, to the 1.5cm indent I originally applied, however if the inserted content already has any indents (e.g. multilevel lists), these are all reset flush to the left.
    2. I also create some tables in one of the RichTextContentControls, some of which are indented to 1.5cm, and have a width of the remainder of the page width, less the indent, and others are either indented having the full width of the page, or are not indented and have the same width. I cannot find any rhyme or reason for the inconsistency in the way Word inserts the tables. I'm inserting the tables in the following manner:
    range = richTextContentControl.Range;
    
    // REPEAT THE FOLLOWING FOR EACH TABLE ITEM
    
    var table = Tables.Add(
        range,
        numRows,
        4,
        Word.WdDefaultTableBehavior.wdWord9TableBehavior,
        Word.WdAutoFitBehavior.wdAutoFitFixed
    );
    var row = 1;
    var space = table.Columns[1].Width - 30;
    table.Columns[1].Width = 30;
    table.Columns[2].Width += space / 2;
    table.Columns[3].Width += space / 2;
    
    // TABLE CONTENT INSERTION METHODS REMOVED FOR BREVITY
    
    range = richTextContentControl.Range;
    range.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
    range.InsertParagraphAfter(); // Must insert a new paragraph to ensure the next table inserted doesn't merge with the previous one.
    range = richTextContentControl.Range;
    range.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
    range.Move(Word.WdUnits.wdCharacter, 1); // Without these two lines the insertion point would be placed outside the content control.
    range.Move(Word.WdUnits.wdCharacter, -1);
    

    I tried resetting the indent before insertion of the table, and this apperts to do nothing, I tried restting the indent after the table is inserted, which appears only the indent the content in the cells of the first column. Either was it seems the results are the same: my table is too wide and isn't indented.

    Can anybody shed some light onto why it might be behaving in this manner, and what I might do to work around it?

    Kind regards, Andrew

    Monday, December 1, 2014 3:18 AM

All replies

  • Hi Andrew,

    Have you tried to use the indent properties of ParagraphFormat?

    http://msdn.microsoft.com/en-us/library/office/dn353027(v=office.15).aspx

    control.Range.ParagraphFormat.LeftIndent =45;
    If it's not working, can you please post some screenshots here so that I can understand you clearly? Or just upload a sample word document in OneDrive?


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Tuesday, December 2, 2014 10:09 AM
    Moderator
  • Hi Caillen

    Thankyou for your response. I have indeed used the LeftIndent property of ParagraphFormat, that is exactly how I tried manually overriding the indent after the call to Range.InsertXML in my first issue listed above. My issue is doing that will reset the left indent for each paragraph in that control to that indent, regardless of what it was originallly. What I was really hoping for, was that it would make the pre-existing indent a minimum, or add it to the left indent of whatever XML I inserted, however it seems to ignore the pre-existing indent altogether, preferring to override it with the inserted format, rather than the other way around. I was hoping that there might be a way to merge styles in a similar manner to the paste options, though I may be able to work around this particular one by manually adding my 1.5cm margin to the value for each paragraph in this content control.

    In the case of the second issue, I haven't found that manually setting the paragraph left indent is of any use to me; if it even works at all I end up with a full-width table pushed well past the right page margin, so I guess that is the more difficult issue to solve.

    Any other ideas? I hope to put together a brief sample doc showing just what I mean when I am at work tomorrow.

    Kind regards, Andrew

    Tuesday, December 2, 2014 11:40 AM
  • Hi Andrew

    I notice this issue is a little over a year old, and I wondered if you'd solved it - because I'm having the same problem.

    Anyway, I think I have a solution.

    Naturally, because its Word vsto it's really stupid, but here goes. What you need is the following line:

    table.Rows.SetLeftIndent([left indent], [ruler style]);

    Not exactly the most intuitive location for it, but it seems to work. The "Ruler Style" parameter governs how the cells are resized if need be (another well-named variable!)

    It works in Word 2010 (what I'm using), and the documentation I found says it works in 2013:

    https://msdn.microsoft.com/en-us/library/office/ff195355.aspx

    Anyway, in case anyone else was looking for a solution to this problem, here it is.

    Chris.

    Monday, December 7, 2015 10:54 PM