Search code examples
c#office-interop

Prevent insert graph within a table with interop word c#


I'm trying to create a document word from scratch with a string, a table and a graph using interop c# word.

Now, I've this code :

        object start = 0;
        object end = 0;
        MSWord.Range rng0 = m_Word_Document.Range(ref start, ref end);


        rng0.Text = "Configuration : " + acquisition.Marche.Configuration.NomConfiguration;
        rng0.Font.Name = "Times New Roman";
        rng0.InsertParagraphAfter();
        rng0.Font.Size = 10.0f;
        rng0.ParagraphFormat.SpaceAfter = 0.0f;

        rng0.Collapse(MSWord.WdCollapseDirection.wdCollapseEnd);
        rng0.InsertParagraphAfter();

        MSWord.Table theTable = m_Word_Document.Tables.Add(rng0, resultat.Poteau_pk.Length + 1, 1);

        theTable.Range.Font.Name = "Times New Roman";
        theTable.Range.Font.Size = 10;
        theTable.Range.ParagraphFormat.SpaceAfter = 0f;
        theTable.Range.Borders.InsideLineStyle = MSWord.WdLineStyle.wdLineStyleSingle;
        theTable.Range.Borders.OutsideLineStyle = MSWord.WdLineStyle.wdLineStyleSingle;

        theTable.Cell(1, 1).Range.Text = "Poteaux";

        for (int i = 0; i < resultat.Poteau_pk.Length; ++i)
        {
            theTable.Cell(i + 2, 1).Range.Text = resultat.Poteau[i].ToString("F1");
        }

        rng0.Collapse(MSWord.WdCollapseDirection.wdCollapseEnd);
        rng0.InsertParagraphAfter();
        rng0.Collapse(MSWord.WdCollapseDirection.wdCollapseEnd);

        m_Chart = AddChart(rng0);

This code creates this document :

enter image description here

I don't know how to set the range outside the table

For me, rng0 doesn't go inside the table..

Thx. Florence.


Solution

  • After creating the table,

    instead of :

        rng0.Collapse(MSWord.WdCollapseDirection.wdCollapseEnd);
        rng0.InsertParagraphAfter();
        rng0.Collapse(MSWord.WdCollapseDirection.wdCollapseEnd);
    

    the code is :

        rng0 = theTable.Range;
        rng0.Collapse(MSWord.WdCollapseDirection.wdCollapseEnd);
        rng0.InsertParagraphAfter();