0

I want to iterate through all bookmarks inside document and set text to each bookmark.Name from datagridview cells values which is already loaded. I'm stuck here in this loop. Please, any suggestions?

using (Novacode.DocX document = DocX.Load(template))
{
    foreach (Novacode.Bookmark bookmark in document.Bookmarks)
    {
        //MessageBox.Show("\tFound bookmarks {0}", bookmark.Name);
        //var bookmarks = bookmark.Name;

        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[0].Value.ToString());

        int i = document.Bookmarks.Count;

        var bookmarks = document.Bookmarks[i].Name;

        document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[0].Value.ToString());
        document.Bookmarks[0].SetText(dataGridViewRow.Cells[1].Value.ToString());
        document.Bookmarks[1].SetText(dataGridViewRow.Cells[2].Value.ToString());
        document.Bookmarks[2].SetText(dataGridViewRow.Cells[3].Value.ToString());
        document.Bookmarks[3].SetText(dataGridViewRow.Cells[4].Value.ToString());

        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[2].Value.ToString());
        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[3].Value.ToString());
        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[4].Value.ToString());
        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[5].Value.ToString());
        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[6].Value.ToString());
        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[7].Value.ToString());
        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[8].Value.ToString());
        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[9].Value.ToString());
        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[10].Value.ToString());
        //document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[11].Value.ToString());
    }                   
    document.SaveAs(path2);
}
diiN__________
  • 7,393
  • 6
  • 42
  • 69
dilesko
  • 21
  • 7
  • Are you saying you know how to set the properties but don't know how to do it in a loop? – Alex Apr 22 '16 at 09:06
  • place `int i = document.Bookmarks.Count` outside loop. – Abhishek Kumar Apr 22 '16 at 10:23
  • @Alex, yes thats correct. I want to set .Name property for all bookmarks inside document with values from datagridview. For example, first found bookmark will get value from dataGridViewRow.Cells[0], second will get from dataGridViewRow.Cells[1], etc...but I'm stuck. – dilesko Apr 22 '16 at 10:37

1 Answers1

0

If I understand you correctly, this is what you are trying to achieve with the loop:

using (Novacode.DocX document = DocX.Load(template))
{
int i = 0;

foreach (Novacode.Bookmark bookmark in document.Bookmarks)
{
    var bookmarks = document.Bookmarks[i].Name;

    document.Bookmarks[bookmark.Name].SetText(dataGridViewRow.Cells[i+1].Value.ToString());

    i++;
}                   
document.SaveAs(path2);
}

What we've done here is declared a variable i which is outside the loop but we increment its value with every foreach iteration. Alternatively, you could rewrite the loop and use a for loop instead:

for(int i=0; i< document.Bookmarks.Count)
{
       //change the code here accordingly
}

Let me know if this helps. Thank you.

Alex
  • 937
  • 3
  • 20
  • 44
  • tnx man, it's working but, when it comes to the last bookmark, it binds last two cell values. I guess this is because of [i+1], then when i put [i] only, it binds previous column. What to do? – dilesko Apr 22 '16 at 12:51
  • @dilesko so it works fine for everything but the last bookmark? – Alex Apr 22 '16 at 14:14
  • yep, when "i" count reaches last bookmark, value from datagridview cells, for example cells[4] is there, plus value from next cells[5]. so basically inside created document on bookmark place is cells[4].value + cells[5].value, instead of only cells[4].value. Do you understand me? :) – dilesko Apr 22 '16 at 17:47
  • @dilesko Yes I understand your query... However, this logic is not consistent with other items right? Everywhere else the difference between bookmark id and cell in gridview was 1. You can avoid doing the last iteration or use if statement in code to cater for this special case – Alex Apr 22 '16 at 19:30
  • Ok, how to avoid this last iteration? Something like document.bookmarks.Count - 1 or? I can send you hole source to check. – dilesko Apr 23 '16 at 07:46
  • Yes in for loop you can say < document.Bookmarks.Count - 1 – Alex Apr 25 '16 at 07:37
  • sorry, can't make it work, index out of range :) probably bad for loop – dilesko Apr 26 '16 at 06:47
  • Ok can you share your code and data somewhere so I can have a look? Thanks – Alex Apr 26 '16 at 08:03
  • 1
    I finally made it. There is something in the inside document, I guess it's a built in bookmark with value "_GoBack". for (int i = 0; i < document.Bookmarks.Count; i++) { if (document.Bookmarks[i].Name != "_GoBack") { string s = dataGridViewRow.Cells[i+1].Value.ToString(); document.Bookmarks[i].SetText(s);Its working,But how to set same value to multiple bookmarks – dilesko May 04 '16 at 09:58
  • @dilesko Congratulations, thank you very much for keeping the community updated! – Alex May 04 '16 at 15:06