This project has moved and is read-only. For the latest updates, please go here.

Find Index to Paragraph/working with existing document

Mar 31, 2012 at 3:42 PM

The example code given focuses on creating a new document, but I need to work with an existing document.  I saw a post on InsertAfterSelf(), but it seems that you need a reference to a "Self" first.  How do I get an index or a reference to an existing paragraph so that I can add a table after it?  How do I locate an existing bullet list and add items?

Apr 1, 2012 at 10:20 PM
Edited Apr 1, 2012 at 10:21 PM

I've only just picked up DocX and started using it so I'm certainly no expert, but when I've had to do something similar I've been able to use one of two approaches:

1) Find some placeholder text in the document and navigate from there

// add the details to the table - find the right table
foreach (var table in docx.Tables)
  // the table has the placeholder text in the first cell of the first non-header row
  if (table.Rows.Count > 1 && table.Rows[1].Cells[0].Paragraphs[0].Text == "@@fulltitlename")
    // you can now do stuff with 'table'

2) Rely on your knowledge of the document structure without relying on placeholders

// find the row to we want
var theRow = docx.tables[2].Rows[6];

// do some things to 'theRow'

I find it useful to set a breakpoint in my code and explore the docx object to get a feel for the document structure.

If you find a better way let me know :)