Insert Text at Bookmark position

Feb 24, 2014 at 6:49 AM
Edited Feb 24, 2014 at 6:51 AM

is it possible to insert new text at the position of an existing bookmark?

If yes, could you please give me a full example?

at the moment i am working with Replace, but its not a very good solution if there is a chance that someone manually likes to change the document.

Here is my actual code:
using (DocX document = DocX.Load(@"C:\temp\readme.docx"))
  document.ReplaceText("strDatum", "24-02-2014");
Hope anyone can help me.

Thank you

Feb 24, 2014 at 6:54 AM
Edited Feb 24, 2014 at 6:56 AM
Some time ago bookmarks were added to main source code (not in the binary):
"This patch adds basic support for inserting and manipulating bookmarks inside a word file. It also allows for inserting text at bookmark locations. The change was predicated on a need to insert text at bookmark locations in a sort of token replacement."

The code is below but this example is taken straight from Source Code where there are multiple examples for different things.
        private static void Bookmarks()

            using (var document = DocX.Create(@"docs\Bookmarks.docx"))
                var paragraph = document.InsertBookmark("firstBookmark");

                var paragraph2 = document.InsertParagraph("This is a paragraph which contains a ");

                paragraph2.InsertAtBookmark("handy ", "secondBookmark");

                Console.WriteLine("\tCreated: docs\\Bookmarks.docx\n");
Unless you mean you need functionality that works for template that already has them then i guess you would need to find the paragraphs first and then try InsertAtBookmark but this wasn't tested and may not be there.
Feb 24, 2014 at 7:53 AM

thanks for your answer.

Yes i am looking for a solution to use a template i got and find the bookmark and insert text.
I was not able to find a paragraph or a bookmark after trying a few hours now.

Is there a solution to do it?

Hope you can help me.

Feb 25, 2014 at 1:28 PM
What about iterate all paragraphs and look up for your specific bookmark?!
            foreach (var paragraph in document.Paragraphs)
                var bookmark = paragraph.GetBookmarks().FirstOrDefault(x => x.Name == "strDatum");
                if (bookmark != null)
                    bookmark.Paragraph.InsertAtBookmark("24-02-2014", bookmark.Name);
Marked as answer by 248907 on 2/27/2014 at 4:10 AM
Feb 26, 2014 at 10:06 AM
would be nice, but:

Error 1 'Novacode.Paragraph' does not contain a definition for 'GetBookmarks' and no extension method 'GetBookmarks' accepting a first argument of type 'Novacode.Paragraph' could be found (are you missing a using directive or an assembly reference?)
Feb 27, 2014 at 10:54 AM
Seems like you're using a wrong version of DocX.dll .. see also:
Marked as answer by 248907 on 2/27/2014 at 4:11 AM
Feb 27, 2014 at 11:10 AM
WOW!, yes.

i DID download the newest compiled Version here and the Thread you mentioned has the same Version but smaller and newer.

Now it works.
Exactly what i needed. Thank you very very much!
Sep 11, 2014 at 9:45 AM
Hi guys,

I'm trying to fill bookmarks of a Word document by the solution provided bjr001 but I have the following issue: GetBookmarks() is not a member of Novacode.Paragraph.

Any solution possible?

Thanks in advance.
Sep 11, 2014 at 12:37 PM
Hi misidra,

download the Version from the Thread mentioned by bjr001 and compile it yourself,
then use the .dll you compiled.
It worked for me, i hope it works for you too.

Let us know.

Sep 11, 2014 at 1:02 PM
I recently try to use that version, but now VS send me this error: Excepción no controlada del tipo 'System.IO.FileNotFoundException' en System.Windows.Forms.dll

Información adicional: Could not load file or assembly 'DocX, Version=, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.

It doesn't work any function.
Sep 11, 2014 at 2:29 PM
Hi misidra,

it says that the system is not able to find your dll. have you copied there from where you linked it in?
Or is the dll where it should be at runtime? where your .exe ist located?

Sep 11, 2014 at 2:58 PM
The .dll file is located at debug folder, like other resources that I'm using.
Sep 12, 2014 at 7:45 AM
from where is the error thrown?
Sep 12, 2014 at 9:14 AM
I've just solved the problem using Interop thing.

Thanks anyway ;)
Sep 12, 2014 at 12:44 PM
@misindra if you're fine with Interop limitations and you know consequences of using it then it's fine. You've been warned :-)
Oct 13, 2014 at 12:20 PM
I used NuGet to install DocX so shouldn't I have the latest version anyway? The link on the other thread didn't work and I don't have the GetBookmarks() or any of the InsertBookmark, AppendBookmark etc code at all??
Oct 17, 2014 at 4:27 PM
Edited Oct 17, 2014 at 4:28 PM
breath2k wrote:
I used NuGet to install DocX so shouldn't I have the latest version anyway? The link on the other thread didn't work and I don't have the GetBookmarks() or any of the InsertBookmark, AppendBookmark etc code at all??
We don't support NuGet deployment as we don't have access there (not sure who is publishing there). So the version as NuGet package may be a lot older then what you get by either downloading newest binary from here or compiling the source code. I would suggest updating the version from binary that is downloadable from here. It was released recently.