none
Cannot insert the OpenXmlElement "newChild" because it is part of a tree. RRS feed

  • Question

  • hi,

    am getting this error(Cannot insert the OpenXmlElement "newChild" because it is part of a tree.) when i try to give border values to the table.

    here is my code,

    if

    (sValue == "")

    {

    InsideVerticalBorder insideVBorder = new InsideVerticalBorder();

    insideVBorder.Val =

    new EnumValue<BorderValues>(BorderValues.None);

    tblBorders.AppendChild(insideVBorder);

    }

    else

    {

    InsideVerticalBorder insideVBorder = new InsideVerticalBorder();

    insideVBorder.Val =

    new EnumValue<BorderValues>(BorderValues.Single);

    tblBorders.AppendChild(insideVBorder);

    }


    banupriya

    Friday, June 14, 2013 5:53 AM

All replies

  • Try using the Append method, rather than AppendChild

    For example:

    private void btnTableWithBorders_Click(object sender, EventArgs e)
    {
        string filePath = @"C:\Users\[name]\Documents\TestCreateTableWithBorders.docx";
        using (WordprocessingDocument pkg = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document))
        {
            MainDocumentPart partDoc = pkg.AddMainDocumentPart();
            partDoc.Document = new Document(new Body());
            Document doc = partDoc.Document;
            Table t = new Table(new TableProperties(new TableStyle() { Val = "TableGrid" }),
                new TableRow(new TableCell(
                new TableCellProperties(new TableCellWidth() { Type = TableWidthUnitValues.Pct, Width = "45%" }),
                new Paragraph(new Run(new RunProperties(new Italic()), new Text("test")))),
                (new TableCell(new Paragraph()))),
                new TableRow(new TableCell(new Paragraph()), new TableCell(new Paragraph())));
            TableProperties tProps = t.Elements<TableProperties>().FirstOrDefault();
            TableBorders tBorders = new TableBorders();
            InsideVerticalBorder ivB = new InsideVerticalBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single) };
            tBorders.Append(ivB);
            tProps.Append(tBorders);
            doc.Body.Append(t);
        }
    


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, June 16, 2013 10:08 AM
    Moderator
  • Thank u for the response !.

    I tried with Append instead of Append child . but still the same problem persists.

    please suggest me some other solution


    banupriya

    Monday, June 17, 2013 6:03 AM
  • Then you need to analyze your code and the result it creates. Since you show us only a small snippet, it's impossible for us to see where your logic may be faulty. I did test the code I posted and it worked just fine. Apparently your node objects are in a complex state where appending isn't possible, but we can't see where that might be... I'm guessing it might have something to do with the node that tblBorders represents.

    Cindy Meister, VSTO/Word MVP, my blog

    Monday, June 17, 2013 3:39 PM
    Moderator
  • i understand the problem . am analysing my code and trying the level best.

     i did map  the table from excel sheet to word document using open xml. some of the table's first row is cutting all the columns in the excel sheet, in the same way it (horizontal merging) should reflect in the word document. but it cuts only the first column. please refer the description below.  A & B are the columns in the excel sheet. "key solution Highlights - single unified solution Transforamtion" is the header row of the sheet and it cuts A & B. the same result (merging the columns)should be there in the word document.

    if you have any code for this it would be fine.

    --------------------------------------------|-----------------------------------------------------

    A                                                     |                                 B

    --------------------------------------------|------------------------------------------------------

     key solution Highlights - single unified solution Transforamtion

    ----------------------------------------------------------------------------------------------------

    ----------------------------------------------------------------------------------------------------

    -----------------------------------------------------------------------------------------------------


    banupriya



    Tuesday, June 18, 2013 6:30 AM
  • my code part :

    private void FindBookmarksAndReplaceWithTable(OpenXmlElement sliblingElement, string text, string partType, object part, System.Data.DataTable dt, WordprocessingDocument activedoc, string stylename) { Run nRun = new Run(); { DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table(); TableProperties props = new TableProperties(); TableBorders tblBorders = new TableBorders(); TopBorder topBorder = new TopBorder(); topBorder.Val = new EnumValue<BorderValues>(BorderValues.Single); tblBorders.Append(topBorder); InsideHorizontalBorder insideHBorder = new InsideHorizontalBorder(); insideHBorder.Val = new EnumValue<BorderValues>(BorderValues.Single); tblBorders.Append(insideHBorder); TableLook tableLook = new TableLook() { Val = "04A0", FirstRow = true, LastRow = false, FirstColumn = true, LastColumn = false, NoHorizontalBand =false, NoVerticalBand = true }; props.Append(tableLook); txt.Text = sValue.ToString(); if (sValue == "") { InsideVerticalBorder insideVBorder = new InsideVerticalBorder(); insideVBorder.Val = new EnumValue<BorderValues>(BorderValues.None); tblBorders.Append(insideVBorder); } else { InsideVerticalBorder insideVBorder = new InsideVerticalBorder(); insideVBorder.Val =new EnumValue<BorderValues>(BorderValues.Single);

    tblBorders.Append(insideVBorder);

    }

    props.Append(tblBorders);



    banupriya


    Tuesday, June 18, 2013 7:44 AM
  • It would help if you'd post code that would reproduce the problem without modification. As it stands, all I could do was copy part of your code into my test procedure, commenting out what can't work for me. Below is what I tested and it runs without error. I don't see any table in the result as your code defines neither rows, nor cells, nor paragraphs. But when I look at the document.xml there is a table defined with the three sets of border properties. So the problem isn't with appending the inside vertical borders, but somewhere else.

            private void btnTableWithBorders_Click(object sender, EventArgs e)
            {
                string filePath = @"C:\Users\Cindy Meister\Documents\TestCreateTableWithBorders.docx";
                using (WordprocessingDocument pkg = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document))
                {
                    MainDocumentPart partDoc = pkg.AddMainDocumentPart();
                    partDoc.Document = new Document(new Body());
                    Document doc = partDoc.Document;
    //CODE FROM POSTER STARTING HERE
                    Run nRun = new Run();
                    {
                        DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table();
                        TableProperties props = new TableProperties();
                        TableBorders tblBorders = new TableBorders();
                        TopBorder topBorder = new TopBorder();
                        topBorder.Val = new EnumValue<BorderValues>(BorderValues.Single);
                        tblBorders.Append(topBorder);
                        InsideHorizontalBorder insideHBorder = new InsideHorizontalBorder();
                        insideHBorder.Val = new EnumValue<BorderValues>(BorderValues.Single);
                        tblBorders.Append(insideHBorder);
                        TableLook tableLook = new TableLook()
                                           {
                                               Val = "04A0",
                                               FirstRow = true,
                                               LastRow = false,
                                               FirstColumn = true,
                                               LastColumn = false,
                                               NoHorizontalBand = false,
                                               NoVerticalBand = true
                                           };
                        props.Append(tableLook);
                        //txt.Text = sValue.ToString();
                        //if (sValue == "")                            {                                    
                        InsideVerticalBorder insideVBorder = new InsideVerticalBorder();
                        insideVBorder.Val = new EnumValue<BorderValues>(BorderValues.Single);
                        tblBorders.Append(insideVBorder);    
    
                        props.Append(tblBorders);
                        table.Append(props);
                        doc.Body.Append(table);
                    }
                }
            }
    


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, June 18, 2013 3:20 PM
    Moderator
  • private void FindBookmarksAndReplaceWithTable(OpenXmlElement sliblingElement, string text, string partType, object part, System.Data.DataTable dt, WordprocessingDocument activedoc, string stylename)
    {
    Run nRun = new Run();
    {
    DocumentFormat.OpenXml.Wordprocessing.Table table = new DocumentFormat.OpenXml.Wordprocessing.Table();
    TableProperties props = new TableProperties();
    TableBorders tblBorders = new TableBorders();
    TopBorder topBorder = new TopBorder(); 
    topBorder.Val = new EnumValue<BorderValues>(BorderValues.Single);                   
    tblBorders.Append(topBorder);  
    InsideHorizontalBorder insideHBorder = new InsideHorizontalBorder();
    insideHBorder.Val = new EnumValue<BorderValues>(BorderValues.Single);
    tblBorders.Append(insideHBorder);
    TableLook tableLook = new TableLook()
                       {
                            Val = "04A0",
                            FirstRow = true,
                            LastRow = false,
                            FirstColumn = true,
                            LastColumn = false,
                            NoHorizontalBand =false,
                            NoVerticalBand = true
                        };
    props.Append(tableLook); 
    int iRowCnt = dt.Rows.Count;
    int iColCnt = dt.Columns.Count ;
    Paragraph pp = new Paragraph();
    DocumentFormat.OpenXml.Wordprocessing.TableStyle tableStyle = new DocumentFormat.OpenXml.Wordprocessing.TableStyle() { Val = "ITIS_RFP_Table" };
    props.Append(tableStyle);                   table.AppendChild<TableProperties>(props);
    for (var i = 0; i < iRowCnt + 1; i++)
    {
    var tr = new DocumentFormat.OpenXml.Wordprocessing.TableRow();
    for (var j = 0; j < iColCnt; j++)
    {
    Paragraph para = new Paragraph();        ParagraphProperties prop = new ParagraphProperties();
    Run rn = new Run();
    Text txt = new Text();
    RunProperties rp = new RunProperties();
    Bold bld = new Bold();
    TableCellProperties tableCellProperties3 = new TableCellProperties();
    TableCellWidth tableCellWidth3 = new TableCellWidth() {
    Width = "2394", Type = TableWidthUnitValues.Auto
    };
    Shading shading3 = new Shading()
    { 
    Val = ShadingPatternValues.Clear, Color = "grey", Fill = "grey", ThemeFill = ThemeColorValues.Accent1
    };
    tableCellProperties3.Append(tableCellWidth3);
    TableCellWidth tableCellWidth3 = new TableCellWidth() { Width = "2394", Type = TableWidthUnitValues.Auto };
    Shading shading3 = new Shading()
    {
    Val = ShadingPatternValues.Clear, Color = "grey", Fill = "grey", ThemeFill = ThemeColorValues.Accent1
    };
    tableCellProperties3.Append(tableCellWidth3);
    if (i == 0)
    {
    sValue = dt.Columns[j].ColumnName.ToString();
    if(sValue == "F1" || sValue == "F2" || sValue == "F3" || sValue == "F4"||sValue == "F5")
    {
    sValue = "";
    }                             tableCellProperties3.Append(shading3);
    Justification justification1 = new Justification()
    { Val = JustificationValues.Center };
    prop.Append(justification1);
    para.Append(prop);
    rp.Append(bld);
    rn.Append(rp);
    }
    else
    {
    sValue = dt.Rows[i - 1][j].ToString();
    }
    var tc = new DocumentFormat.OpenXml.Wordprocessing.TableCell();
    tc.Append(tableCellProperties3);
    txt.Text = sValue.ToString();
    rn.Append(txt);
    para.Append(rn);
    tc.Append(para);
    ApplyStyleToParagraph(activedoc, stylename, stylename, para);
    tr.Append(tc);                   
    if (sValue == "")                            {                                    
    InsideVerticalBorder insideVBorder = new InsideVerticalBorder();
    insideVBorder.Val = new EnumValue<BorderValues>(BorderValues.None);
    tblBorders.Append(insideVBorder);
    }
    else
    {
    InsideVerticalBorder insideVBorder = new InsideVerticalBorder();
    insideVBorder.Val =new EnumValue<BorderValues>(BorderValues.Single);tblBorders.Append(insideVBorder);
    }    
    props.Append(tblBorders);
    table.Append(tr);
    nRun.Append(table);
    pp.Append(nRun);
    sliblingElement.InsertBeforeSelf(pp);

    yes, i did not post entire code here . but i got errors in this part .if you want full code of that particular function  i can put it here for ur testing purpose.


    banupriya

    Wednesday, June 19, 2013 4:59 AM
  • Hi banupriya

    <sigh> and EXACTLY which line of code in what you posted causes the error message?


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, June 19, 2013 10:18 AM
    Moderator
  • I resolved the same problem.

    the reason is  a OpenXmlElement  was append twice or more.

    just check your logic.  such as :

    ----------------------------------------------

    Run run=new Run();

    Pagraph p=new Pagraph();

    p.Append(run);

    p.Append(run);

    -------------------------------------------

    you should  moidy the code as below:

    -----------------------------------------------

    Run run=new Run();

    Pagraph p=new Pagraph();

    p.Append(run);

    p.Append(run.Clone()); //  p.Append(run.CloneNode(true));

    ----------------------------------------------------


    Wednesday, March 4, 2015 8:44 PM
  • It is very kind of you! I have spend one day to solve it until see your answer! Many thanks!
    Friday, September 18, 2015 2:59 AM