Insert Tables into a Document

Hi,
I’m trying to insert several Tables into a Document.
This code works fine for one table, but fails for more than one:

private void _insertTable(Table table)
{
    foreach (Section sect in _dstDoc.Sections)
    {
        foreach (Paragraph paragraph in sect.Body.Paragraphs)
        {
            foreach (Run run in paragraph.Runs)
            {
                switch (run.Text.Trim())
                {
                    case Globals.ITEMS_RATINGS:
                        Section sectItemsNew = new Section(_dstDoc);
                        Body bodyNewItems = new Body(_dstDoc);
                        sectItemsNew.AppendChild(bodyNewItems);
                        bodyNewItems.Tables.Add(table);
                        _dstDoc.InsertBefore(sectItemsNew, sect);
                        run.Text = "";
                        break;
                    case Globals.CATEGORY_SUMMARY:
                        Section sectCategoryNew = new Section(_dstDoc);
                        Body bodyCategoryNew = new Body(_dstDoc);
                        sectCategoryNew.AppendChild(bodyCategoryNew);
                        bodyCategoryNew.Tables.Add(table);
                        _dstDoc.InsertAfter(sectCategoryNew, sect);
                        run.Text = "";
                        break;
                }
            }
        }
    }
}

Also tried this code, but again it fails for more than one table:

private void _insertTable(Table table)
{
    foreach (Section sect in _dstDoc.Sections)
    {
        foreach (Paragraph paragraph in sect.Body.Paragraphs)
        {
            foreach (Run run in paragraph.Runs)
            {
                switch (run.Text.Trim())
                {
                    case Globals.ITEMS_RATINGS:
                        sect.Body.InsertAfter(table, paragraph);
                        sect.Body.InsertBefore(table, paragraph);
                        sect.Body.Tables.Insert();
                        paragraph.Remove();
                        run.Text = "";
                                    break;
                                case Globals.CATEGORY_SUMMARY:
                                    sect.Body.Tables.Add(table);
                                    sect.Body.InsertAfter(table, paragraph);
                                    sect.Body.InsertBefore(table, paragraph);
                                    paragraph.Remove();
                                    run.Text = "";
                                    break;
                            }
                        }
                    }
                }
            }

Thanks,
Ophir

Please specify what kind of exception happens and at what line of code.

There is no Exception, but of the two happens:

  1. The Second inserted table inserted is not visible in Word, or visible only when the cursor is on top of it.
  2. The tables are inserted one on top of the other, and are again visible partly.

I attached a document for example.
Thanks,
Ophir

You should not insert the same table more than once. If you want to insert the same table in several places of the document - then clone it using Clone method.

Here is a workable example:

ArrayList paragraphsToRemove = new ArrayList();
foreach (Section sect in _dstDoc.Sections)
{

    foreach (Paragraph paragraph in sect.Body.Paragraphs)
    {
        foreach (Run run in paragraph.Runs)
        {
            switch (run.Text.Trim())
            {
                case "ITEMS_RATINGS":
                    sect.Body.InsertAfter(table.Clone(true), paragraph);
                    paragraphsToRemove.Add(paragraph);
                    break;
                case "CATEGORY_SUMMARY":
                    sect.Body.InsertAfter(table.Clone(true), paragraph);
                    paragraphsToRemove.Add(paragraph);
                    break;
            }
        }
    }
}
foreach (Paragraph paragraph in paragraphsToRemove)
{

    paragraph.Remove();
}

Still not working.
I’m not inserting the same table again,
but creating a new table and inserting it to the same Document.
You can see in the attached document that the first inserted table is OK,
but the second one(on the second page) is not visible, until the mouse cursor is on it,
or when moving the page up and down.
Thanks,
Ophir

Code like

sect.Body.InsertAfter(table, paragraph);
sect.Body.InsertBefore(table, paragraph);

effectively inserts the same table two times.
If you have more than one occurence of Globals.ITEMS_RATINGS and Globals.CATEGORY_SUMMARY in the document the number of times the same table gets inserted into the document becomes even bigger.
Please attach your zipped project together with template documents (if any). Then I can help you further.