Insert Table at field

Posted by: david.sheppard on 9 February 2020, 9:09 pm EST

  • Posted 9 February 2020, 9:09 pm EST

    I am trying to add a table at the location of a complexfield using the following code.

    tbl1 = ComplexField1.GetRange().Tables.Insert(location:=InsertLocation.After)

    The error that I get is:

    GrapeCity.Documents.Word.InvalidParentContentException: 'Cannot insert content of type 'Table' into parent content of type 'Paragraph' because the content to insert can be a child only for the folowing content types: Body, Cell, ControlContent.'
  • Replied 10 February 2020, 10:53 pm EST

    Hi David,

    Thank you for the code snippet. Using this code, I too got the exception and it looks like a limitation. So, I have forwarded it to the developer [Doc-1719].
    I will keep you updated with the progress on this.

    Thanks.
    Ruchir
  • Replied 10 February 2020, 11:26 pm EST

    Thank you for your response. My preference would be for the command to insert a table to work like the insert command for new text. This is the command to insert text:

    ComplexField1.GetRange().Runs.Insert("AAA", InsertLocation.Before)

    The command to insert a table should look like:

    Dim tbl1 as table
    tbl1 = ComplexField1.GetRange().Tables.Insert(Columns as integer, Rows as integer, InsertLocation as InsertLocation) as table
  • Replied 11 February 2020, 6:18 pm EST

    Hi David,

    Thanks, I have shared the suggestion with the developers and as soon as I receive an update, I will let you know.

    Thanks,
    Ruchir
  • Marked as Answer

    Replied 20 February 2020, 4:34 am EST

    Hi David,

    Please try the following code instead:

    doc.Body.ComplexFields.First.GetRange().Paragraphs.Last.GetRange().Tables.Insert(2, 2, InsertLocation.After);

    The reason is that a complex field's range ends at the last run. A table cannot be added after a run, but only after a paragraph. Hence the error in your original code. Hopefully the modified code above will work, please let me know.

    Thanks,
    Dmitry.
  • Replied 20 February 2020, 4:50 am EST

    That works great. Thank you.
  • Replied 20 February 2020, 4:53 am EST

    Excellent, thanks for the heads up!
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels