0

I am using Novacode docx to read bookmarks and replace them with my data. Looking at my docx file, I have a bookmark called 'UserID' and I try and replace it in my c# code as below

using (DocX doc = DocX.Load(oTemplate))
{
    doc.Bookmarks["UserID"].SetText("MyUser");
}

The error I get is "sequence contains more than one element" When I look in the debugger the object of doc, there are actually two bookmarks with the name 'UserID' but in word there is only one. I am using word 2016. Anybody has any ideas what the problem could be?

user2837961
  • 1,505
  • 3
  • 27
  • 67
  • Is it possible that the first bookmark is in your table of contents, and the second is your actual bookmark? BTW: I would interested if you get this working fully as I also have a need to remove a whole bookmark section. – jason.kaisersmith Jul 07 '17 at 06:15

1 Answers1

-1
            using (DocX document = DocX.Load(AchivoPlantilla))
            {

                Novacode.Image img = document.Images[0];

                int i = 0;
                foreach (Novacode.Bookmark bookmark in document.Bookmarks)
                {
                    var bookmarks = document.Bookmarks[i].Name;
                    if (bookmarks == "varNombreEmpleado") //Compara la marca con la varible de base de datos
                    {
                        document.Bookmarks[bookmark.Name].SetText(TextBox1.Text);

                    }
                    i++;
                }
                document.SaveAs(docSalida);
                Process.Start("WINWORD.EXE", "\"" + docSalida + "\"");
            }