confusedandamused confusedandamused - 17 days ago 5
C# Question

Insert List<string> into word document

I'm currently trying to find a way to read in, and insert data into a word document. So far this is what I have gotten:

class Program
{
static void Main(string[] args)
{
var FileName = @"C:\temp\test.DOC";
List<string> data = new List<string>();
Application app = new Application();
Document doc = app.Documents.Open(@"C:\temp\test.DOC");

foreach (Paragraph objParagraph in doc.Paragraphs)
{
data.Add(objParagraph.Range.Text.Trim());
}
//data.Insert


data.Insert(16, "Test 1");
data.Insert(16, "\tTest 2\tName\tAmount");
data.Insert(16, "Test 3");
data.Insert(16, "Test 4");
data.Insert(16, "Test 5");
data.Insert(16, "Test 6");
data.Insert(16, "Test 7");
data.Insert(16, "Test 8");
data.Insert(16, "Test 9");
data.Insert(16, "Test 10");

var x = doc.Paragraphs.Add();
x.Range.Text.Insert(0,"\tTest 2\tName\tAmount");

doc.SaveAs2(@"C:\temp\test3.DOC");

((_Document)doc).Close();
((_Application)app).Quit();

}
}


Now, this successfully populates the List data - but I'm trying to append each new test element at the [16]th index, and save it into the word document. Is there a simple way to accomplish this, or am I just over-thinking this issue?

I realize the string list is separate from the Document object which represents the word document.

I have a few other places in the document where I am using bookmarks to add data, but I don't think it is possible to use bookmarks for placing the data in this instance - or If I don't have to use bookmarks I'd like to stray away from that.

EDIT: I am trying to insert X amount of elements at the [16]th position within the data[].

EDIT 2:
Essentially I am sourcing the data dynamically, and I'm not sure how many records/rows I'll need to add to the document, so it could be as follows:

[15]
[16]\tName\tID\tAMOUNT
[17]\tName\tID\tAMOUNT
[18]\tName\tID\tAMOUNT


Since the headers will already be there (NAME,ID,AMOUNT), and each time I run the program I'm not sure how many elements I'll be inserting into the document - so as long as each element is placed under one another, and on the 16th line in the document template I have setup that should accomplish what I am trying to do.

Image 1 - Image into string array
Image 2 - Image after adding content into the string - this is what the resulting document. (this is to be saved)

Image into string array
Image after adding content into the string - this is what the resulting document

enter image description here
I'm attempting to put each element ie: Test1 Test2 Test3 in their each own column each (see above)

Answer

Again I am totally confused as to why you want to read the word file into a string list array. This simply adds the text you show after line 15 into the word document. You do not specify WHERE Test 1, Test 2, Test3... are coming from.

static void Main(string[] args)
{
  List<string> data = new List<string>();
  Application app = new Application();
  Document doc = app.Documents.Open(@"C:\temp\test.DOC");

  string testRows = "Test 1\nTest 2\nTest 3\nTest 4\nTest 5\nTest 6\nTest 7\nTest 8\nTest 9\nTest 10\n";

  var x = doc.Paragraphs[16];
  x = doc.Paragraphs.Add(x.Range);
  x.Range.Text = testRows;

  doc.SaveAs2(@"C:\temp\test3.DOC");

  ((_Document)doc).Close();
  ((_Application)app).Quit();
}
Comments