none
Issue while copying table after a table in word using Interop.Word, range.FormatedText RRS feed

  • Question

  • We are using Interop.Word for creating a document, we need to write a table after that we need to paste a table from a document.

    What is happening when we paste this second table it gets automatically merged with first table.

    Even Document.xml changes the node structure. We also tried putting some w:p paragraph node after the created table but it’s not helping us,

    can you please help us in resolving this.

     

    Versions: C# 3.5 and Word 2007 

     

    The function to write a table,

    public static void AppendL(string docu, string sed)
            {
                using (WordprocessingDocument wd = WordprocessingDocument.Open(docu, true))
                {
                    NameTable nt = new NameTable();
                    XmlNamespaceManager nsm = new XmlNamespaceManager(nt);
                    nsm.AddNamespace("w", Consts.wmlns);
                    XmlDocument inx = new XmlDocument();
                    inx.Load(wd.MainDocumentPart.GetStream());
                    XmlNode body = inx.SelectSingleNode("//w:body", nsm);
    
                    XmlNode p_outer = inx.CreateNode(XmlNodeType.Element, "w:p", Consts.wmlns);
                    XmlNode r_outer = inx.CreateNode(XmlNodeType.Element, "w:r", Consts.wmlns);
                    XmlNode t_outer = inx.CreateNode(XmlNodeType.Element, "w:t", Consts.wmlns);
    
                    XmlElement ps_outer = inx.CreateElement("w:pStyle", Consts.wmlns);
                    ps_outer.SetAttribute("val", Consts.wmlns, "Normal");
                    XmlElement jc_outer = inx.CreateElement("w:jc", Consts.wmlns);
                    jc_outer.SetAttribute("val", Consts.wmlns, "right");
                    XmlNode pp_outer = inx.CreateNode(XmlNodeType.Element, "w:pPr", Consts.wmlns);
                    XmlElement rr_outer = inx.CreateElement("w:rPr", Consts.wmlns);
    
                    pp_outer.AppendChild(jc_outer);
                    pp_outer.AppendChild(rr_outer);
                    r_outer.AppendChild(rr_outer);
                    r_outer.AppendChild(t_outer);
                    p_outer.AppendChild(pp_outer);
                    p_outer.AppendChild(r_outer);
    
                    XmlNode tbl = inx.CreateNode(XmlNodeType.Element, "w:tbl", Consts.wmlns);
                    XmlNode tblPr = inx.CreateNode(XmlNodeType.Element, "w:tblPr", Consts.wmlns);
    
                    XmlElement tblW = inx.CreateElement("w.tblW", Consts.wmlns);
                    tblW.SetAttribute("w", "9497");
                    tblW.SetAttribute("type", "dxa");
    
                    XmlElement tblLook = inx.CreateElement("w.tblLook", Consts.wmlns);
                    tblW.SetAttribute("val", "04A0");
    
    
                    XmlNode tr = inx.CreateNode(XmlNodeType.Element, "w:tr", Consts.wmlns);
                    //XmlNode trPr = inx.CreateNode(XmlNodeType.Element, "w:trPr", Consts.wmlns);
    
                    XmlNode tc = inx.CreateNode(XmlNodeType.Element, "w:tc", Consts.wmlns);
                    XmlNode tcPr = inx.CreateNode(XmlNodeType.Element, "w:tcPr", Consts.wmlns);
    
                    XmlNode p = inx.CreateNode(XmlNodeType.Element, "w:p", Consts.wmlns);
                    XmlNode r = inx.CreateNode(XmlNodeType.Element, "w:r", Consts.wmlns);
                    XmlNode t = inx.CreateNode(XmlNodeType.Element, "w:t", Consts.wmlns);
    
                    XmlElement ps = inx.CreateElement("w:pStyle", Consts.wmlns);
                    //ps.SetAttribute("val", Consts.wmlns, "BodyText");
                    XmlAttribute att = inx.CreateAttribute("w:val");
                    att.Value = "right";
    
                    //XmlElement ele = inx.CreateElement("w:pPr");
                    //inx.AppendChild(ele);
                    //ele.Attributes.Append(att);
    
                    
                    t.InnerText = "Section Effective Date:" + sed;
                    ps.SetAttribute("val", Consts.wmlns, "Normal");
                    XmlElement jc = inx.CreateElement("w:jc", Consts.wmlns);
                    jc.SetAttribute("val", Consts.wmlns, "right");
                    
                   
                    XmlElement tbljc = inx.CreateElement("w:jc", Consts.wmlns);
                    tbljc.SetAttribute("val", Consts.wmlns, "right");
                    XmlElement i = inx.CreateElement("w:i", Consts.wmlns);
                    XmlElement sz = inx.CreateElement("w:sz", Consts.wmlns);
                    sz.SetAttribute("val", Consts.wmlns, "16");
                    XmlElement sc = inx.CreateElement("w:szCs", Consts.wmlns);
                    sc.SetAttribute("val", Consts.wmlns, "16");
                    XmlElement rr = inx.CreateElement("w:rPr", Consts.wmlns);
                    //XmlElement rr1 = inx.CreateElement("w:pStyle", Consts.wmlns);
                    XmlNode pp = inx.CreateNode(XmlNodeType.Element, "w:pPr", Consts.wmlns);
                    
                    rr.AppendChild(i);
                    rr.AppendChild(sz);
                    rr.AppendChild(sc);
                    pp.AppendChild(ps);
                    pp.AppendChild(jc);
                    pp.AppendChild(rr);
                    r.AppendChild(rr);
                    r.AppendChild(t);
                    p.AppendChild(pp);
                    p.AppendChild(r);
                    //body.AppendChild(p); //Kezar part 1
    
                    
                    //trPr.AppendChild(jc);
                    //tr.AppendChild(trPr);
                    tblPr.AppendChild(tblW);
                    tblPr.AppendChild(tbljc);
                    tblPr.AppendChild(tblLook);
                    tbl.AppendChild(tblPr);
                    tc.AppendChild(p);
                    tr.AppendChild(tc);
                    tbl.AppendChild(tr);
                    body.AppendChild(tbl);
    
                    //p_outer.AppendChild(tbl);
                    //body.AppendChild(p_outer);
                    //XmlElement br = inx.CreateElement("w:br", Consts.wmlns);
                    //br.SetAttribute("w:type", "text-wrapping");
                    //body.AppendChild(br);
    
                    XmlNode p1 = inx.CreateNode(XmlNodeType.Element, "w:p", Consts.wmlns);
                    XmlNode r1 = inx.CreateNode(XmlNodeType.Element, "w:r", Consts.wmlns);
                    XmlNode t1 = inx.CreateNode(XmlNodeType.Element, "w:t", Consts.wmlns);
                    t1.InnerText = "";
                    XmlElement ps1 = inx.CreateElement("w:pStyle", Consts.wmlns);
                    ps1.SetAttribute("val", Consts.wmlns, "Normal");
                    XmlNode pp1 = inx.CreateNode(XmlNodeType.Element, "w:pPr", Consts.wmlns);
                    
                    r1.AppendChild(t1);
                    pp1.AppendChild(ps1);
                    p1.AppendChild(pp1);
                    p1.AppendChild(r1);
                    
                    body.AppendChild(p1);
                    XmlElement br = inx.CreateElement("w:br", Consts.wmlns);
                    br.SetAttribute("w:type", "text-wrapping");
                    //body.AppendChild(br);
    
                    inx.Save(wd.MainDocumentPart.GetStream(FileMode.Create));
                    wd.Close();
                }
            }
    
    

     

    Code part using FormatedText

    if (Path.GetExtension(n.GetAttribute("url")) == ".docx")
                        {
                            DXFile.AppendL(output, n.GetAttribute("EffDate"));
    
                            object s = Path.GetFullPath(output);
                            object s1 = inP + Path.GetFileName(n.GetAttribute("url"));
    
                            try
                            {
                                wd = a.Documents.Open(ref s, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u);
                                wd1 = a.Documents.Open(ref s1, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u);
    
                                wd.Activate();
                                wd.TrackRevisions = false;
                                wd.ShowRevisions = false;
    
                                object wds = wd.Content.End - 1;
                                object wde = wd.Content.End;
                                object st = wd1.Content.Start;
                                object en = wd1.Content.End;
    
                                Range r = wd.Range(ref wds, ref wde);
    
                                //Table table = wd.Tables.Add(r, 1, 1, ref u, ref u);
                                //Range tableRange = table.Rows[1].Cells[1].Range;
    
                                Range sourceRange = wd1.Range(ref st, ref en);
                                //sourceRange.InsertParagraphBefore();
    
                                r.FormattedText = sourceRange;
                                                            
                                foreach (Paragraph para in r.Paragraphs)
                                {
                                    para.OutlineLevel = WdOutlineLevel.wdOutlineLevelBodyText; //added for PVC fix, sub heading indentation
                                }
    
    

     

     

     


    • Edited by Kezar Mader Friday, November 11, 2011 8:43 AM
    Friday, November 11, 2011 8:30 AM

Answers

  • Hi Kezar

    Your code for the interop is not readable without comments about what it's supposed to be doing - we can only guess, but that's not good enough for trouble-shooting...

    I'm going to assume that the source document contains a single table and that this should be inserted at the end of the target document. Further, at the end of the target document there is currently a table. So a paragraph mark is required after that table, and the source table needs to be inserted after that, but before the end-of-document paragraph mark.

    In that case, the (pseudo-) code would look something like this:

    rngTableSource = wd.Tables[1].Range
    rngTarget = wd1.Content;
    rngTarget.InsertAfter("\r");
    object collapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
    rngTarget.Collapse(ref collapseEnd);
    rngTarget.FormattedText = rngTableSource.FormattedText;


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Kezar Mader Friday, November 11, 2011 10:37 AM
    Friday, November 11, 2011 9:44 AM
    Moderator

All replies

  • Hi Kezar

    The resolution of the screen shot is too poor to make out the code you're using. Please paste it into the "code" tool (second button from the right on the forum toolbar) so that we can see it.

    Please also specify the version of Word you're using.


    Cindy Meister, VSTO/Word MVP
    Friday, November 11, 2011 8:33 AM
    Moderator
  • Hi,

    Please check the updated main text, added the code in that.

    Thanks,

    Friday, November 11, 2011 9:16 AM
  • This is also using Interop.Word, if you see second code snipet,

    wd = a.Documents.Open(ref s, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u);
    wd1 = a.Documents.Open(ref s1, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u, ref u);

    Friday, November 11, 2011 9:41 AM
  • Hi Kezar

    Your code for the interop is not readable without comments about what it's supposed to be doing - we can only guess, but that's not good enough for trouble-shooting...

    I'm going to assume that the source document contains a single table and that this should be inserted at the end of the target document. Further, at the end of the target document there is currently a table. So a paragraph mark is required after that table, and the source table needs to be inserted after that, but before the end-of-document paragraph mark.

    In that case, the (pseudo-) code would look something like this:

    rngTableSource = wd.Tables[1].Range
    rngTarget = wd1.Content;
    rngTarget.InsertAfter("\r");
    object collapseEnd = Word.WdCollapseDirection.wdCollapseEnd;
    rngTarget.Collapse(ref collapseEnd);
    rngTarget.FormattedText = rngTableSource.FormattedText;


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Kezar Mader Friday, November 11, 2011 10:37 AM
    Friday, November 11, 2011 9:44 AM
    Moderator
  • Thanks Cindy, it has resolved the issue, Thanks a lot for quick solution.

    Friday, November 11, 2011 10:38 AM