This project has moved. For the latest updates, please go here.

Dynamically Inserting lists at specified location within a word document

Mar 26, 2014 at 3:57 PM
Edited Mar 26, 2014 at 4:12 PM
I am using an existing word document as a “template” and replacing a bunch of text. A new requirement came up where now I need to be able to add a series of bulleted list in the document at specified sections of the document.

I followed the example on how to add a list using docx and that appears to work but what I cannot figure out is how do I tell docx where to place my list it (it’s always appending it at the end of the document) I also tried the overloaded method InsertList(index, List) but it’s throwing an exception (ArgumentOutOfRangeException) on “internal static Paragraph GetFirstParagraphEffectedByInsert(DocX document, int index)”.

I cannot figure out what “index” it needs or what I am missing.

Does anyone have an example to share how they were able to add lists in an existing doc in a specified locations. Or can someone point me in the right direction.

Thanks in advance

Mar 29, 2014 at 4:48 PM
Edited Mar 29, 2014 at 4:51 PM
I did a little more digging around but I am having difficulty to understand if the functionally is actually there or if it is lacking. So I started looking a little closer.

For example the DocX class has a Paragraphs list object and anytime you load a word document or insert a paragraph the List is updated. I also noticed a paragraphLookup dictionary (also in the docX class) which incidentally is referenced by the GetFirstParagraphEffectedByInsert (this is somewhat puzzling).

When I load and existing document the __paragraphLookup __ dictionary does not appear to be loaded which I think may explains why I am getting a ArgumentOutOfRangeException when I try to either insert paragraph or list at a certain location in the document.

So it dawned to me that maybe at the time of load we need to also initialize the paragraphLookup thinking that once it’s loaded everything will just begin working magically. See sample code in load(String fileName) method.
// Initialize the paragraphLookup dictionary at load
List<Paragraph> list = document.Paragraphs;
foreach (Paragraph p in list) {
    document.paragraphLookup.Add(p.startIndex, p);
Well it turns out that after I implemented it the exception is still occurring. When I try to insert a new paragraph the __paragraphLookup __ dictionary does not appear to ever get updated. Meaning that if the document has 2 paragraphs at the time of load the paragraphs and paragraphLookup count will be at 2 but once I insert a new paragraph at runtime the count in paragraphs is 3 and count in paragraphLookup is 2 which blew my theory completely out of the water. Now I am thinking that maybe at the time an insert is performed affecting a paragraph the dictionary needs to be refreshed from scratch, but is this the right approach?

This leads to believe that I am not following the implementation of the original designer. So Could someone for the original design team comment on the approach and point me in the right direction?

I can try to write a patch but what I am seeing at the moment is that it can potentially affect many methods . And I am hesitant to go down a rabbit hole that can cause more harm than good.

As always thanks in advance for any feedback