Agregar dos tablas a un documento de Word directamente una encima de la otra

Word has this feature where it merges tables that are inserted together, so to add two separate tables, you have to put a paragraph in between them. This has been working well, but now I need to make the paragraph go away after making the tables separate so that they're stacked on top of each other.

var document = GetDocument(word, @"C:\Blank.docx");
object missing = System.Reflection.Missing.Value;

var paragraph = document.Paragraphs.Add(ref missing);
paragraph.Range.Text = "..."; 

var table = document.Tables.Add(paragraph.Range, 1, 1, ref missing, ref missing);
table.Borders.Enable = 1;


paragraph = document.Paragraphs.Add(ref missing);
paragraph.Range.Text = "...";
table = document.Tables.Add(paragraph.Range, 1, 1, ref missing, ref missing);
table.Borders.Enable = 1;


var doc_close = (Microsoft.Office.Interop.Word._Document)document;


The top picture shows the result of the above code. I want to remove the paragraph between the two tables but don't know how to using the com object. One important thing to note is that to delete the paragraph in word, you have to press "Delete" and not "Backspace". I think that is an important hint to find the correct answer.

The top picture is the result of the above code, the bottom picture is what I want.

This example makes it seem like I want to do something that could be done with a single table, but the actual application works differently and requires two different tables that are stacked on top of each other.

preguntado el 09 de marzo de 12 a las 15:03

1 Respuestas

I would try using this Lib I found it a while back looks alot easier to use than com

You can always use the openxml SDK for office as well but I suspect docx will be easier

respondido 16 mar '12, 22:03

I will use this with my next project for sure, com is a head-ache and a half! For this project, I've already coded everything using com, this paragraph is the only thing that is in my way to complete this. (It's driving me crazy! lol) - sorpresa

Sad face, DocX appears to suffer from the same problems. I tried adding two tables to the same document, and word just merges them into one. Then I tried adding two tables with a paragraph between them and now I can't remove the paragraph... I'm still keeping up hope, this is a lot easier to use than com. - sorpresa

were you able to use docx to do what you wanted to do ? - Micah Armantrout

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.