0% found this document useful (0 votes)
694 views

Insert Text at Bookmark Position

The document discusses inserting text at bookmark positions in a Word document using the DocX library. It provides an example of using the InsertAtBookmark method to insert text at the position of an existing bookmark and discusses issues users had with older library versions not containing the necessary methods.

Uploaded by

Igor Gjorgjiev
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
694 views

Insert Text at Bookmark Position

The document discusses inserting text at bookmark positions in a Word document using the DocX library. It provides an example of using the InsertAtBookmark method to insert text at the position of an existing bookmark and discusses issues users had with older library versions not containing the necessary methods.

Uploaded by

Igor Gjorgjiev
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DocX - Insert Text at Bookmark position

CodePlex

HOME

Register

Project Hosting for Open Source Software

SOURCE CODE

DOWNLOADS

DOCUMENTATION

DISCUSSIONS

Search all projects

Sign In

ISSUES

PEOPLE


New
Thread

Insert Text at Bookmark position

Wiki Link: [discussion:534685]


Hello,

248907
Feb 24, 2014 at 7:49

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

AM
Edited Feb 24, 2014

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

at 7:51 AM

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");
document.Save();
}
Hope anyone can help me.
https://docx.codeplex.com/discussions/534685[30/03/2015 21:59:04]

LICENSE

Subscribe

DocX - Insert Text at Bookmark position

Thank you
Reto

MadBoy
Developer

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."

Feb 24, 2014 at 7:54


AM

The code is below but this example is taken straight from Source Code where there are multiple examples for different things.

Edited Feb 24, 2014


at 7:56 AM

private static void Bookmarks()


{
Console.WriteLine("\nBookmarks()");
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.AppendBookmark("secondBookmark");
paragraph2.Append("bookmark");
paragraph2.InsertAtBookmark("handy ", "secondBookmark");
document.Save();
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.
Hello,

248907
Feb 24, 2014 at 8:53

thanks for your answer.

https://docx.codeplex.com/discussions/534685[30/03/2015 21:59:04]

DocX - Insert Text at Bookmark position


AM

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.
Reto
What about iterate all paragraphs and look up for your specific bookmark?!

bjr001
Feb 25, 2014 at 2:28

foreach (var paragraph in document.Paragraphs)

PM

{
var bookmark = paragraph.GetBookmarks().FirstOrDefault(x => x.Name ==
"strDatum");
if (bookmark != null)
bookmark.Paragraph.InsertAtBookmark("24-02-2014", bookmark.Name);
}

would be nice, but:

248907
11:06 AM

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?)

bjr001

Seems like you're using a wrong version of DocX.dll .. see also:


https://docx.codeplex.com/discussions/534409

Feb 26, 2014 at

Feb 27, 2014 at


11:54 AM

WOW!, yes.

248907
Feb 27, 2014 at
12:10 PM

i DID download the newest compiled Version here 1.0.1.3 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!
Hi guys,

https://docx.codeplex.com/discussions/534685[30/03/2015 21:59:04]

DocX - Insert Text at Bookmark position

misidra
Sep 11, 2014 at
10:45 AM

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.
Hi misidra,

248907
Sep 11, 2014 at 1:37
PM

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.
Greetings
Reto

misidra

I recently try to use that version, but now VS send me this error: Excepcin no controlada del tipo 'System.IO.FileNotFoundException'
en System.Windows.Forms.dll

Sep 11, 2014 at 2:02


PM

Informacin adicional: Could not load file or assembly 'DocX, Version=1.0.1.13, Culture=neutral, PublicKeyToken=null' or one of its
dependencies. The system cannot find the file specified.
It doesn't work any function.
Hi misidra,

248907
Sep 11, 2014 at 3:29
PM

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?
saludos,
Reto
The .dll file is located at debug folder, like other resources that I'm using.

misidra
Sep 11, 2014 at 3:58
PM

from where is the error thrown?


https://docx.codeplex.com/discussions/534685[30/03/2015 21:59:04]

DocX - Insert Text at Bookmark position

248907
Sep 12, 2014 at 8:45
AM

I've just solved the problem using Interop thing.

misidra
Sep 12, 2014 at

Thanks anyway ;)

10:14 AM

@misindra if you're fine with Interop limitations and you know consequences of using it then it's fine. You've been warned :-)

MadBoy
Developer

Sep 12, 2014 at 1:44


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??

breath2k
Oct 13, 2014 at 1:20
PM

breath2k wrote:

MadBoy
Developer

Oct 17, 2014 at 5:27


PM
Edited Oct 17, 2014
at 5:28 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??
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.

Sign in to post message or set email notifications

2006-2015 Microsoft

Get Help

https://docx.codeplex.com/discussions/534685[30/03/2015 21:59:04]

Privacy Statement

Terms of Use

Code of Conduct

Advertise With Us

Version 12.4.2014.20959

You might also like