1
votes

What is the best way to insert alot of text into a word document? I have a predefined template that has a header, title page, etc, and I want to insert text at a certain point. You see, I am querying a database for this text. If I query the DB and then insert the database at a bookmark, it inserts it in reverse order. For instance if I query my database for the first two rows of...

Column A      Column B
Text1         1
Text2         2

It will appear Text2 2 Text1 1 at the bookmark. I've also tried looking for a "variable" in the document (ie: #variable) and using Find.Replace with each query, then adding #variable to the end. I am having terrible trouble formatting it though. Any thoughts?

2

2 Answers

2
votes

I have written something that does this in this way - using bookmarks - but gets around the reverse problem by reading the fields in reverse, meaning that the document comes out in the right order.

I am not saying that this is the best solution, as a rule, but that it is consistent enough to work this way.

0
votes

Rather than just inserting it straight in you can always place the results into a list then sort the list. That way you can insert the text in the right order.

If you want it in the order it appears in the database then you can sort it by creating a second list, then starting from the end of list 1 place each item into list 2. That way you will have reversed the order.

It would look something like this:

for (int i = (Results.Count - 1); i >= 0; i--)
{
    list2.Add(list1.Items[i]);
}

Hopefully this helps if you can't do what the first answer said.