Word Bookmark

  • Thread starter Thread starter Alan T
  • Start date Start date
A

Alan T

How do I make use of the Bookmarks property so that I can write a text
at/below the position of a particular bookmark or the first bookmark ?

private Microsoft.Office.Interop.Word.Document myWordDoc = new Document();

myWordDoc.Bookmarks
 
Alan,

In the following code, applicationObject refers to the Word application. The
code targets the application's ActiveDocument, but you can easily modify
this to target the document referenced by myWordDoc.

The following code inserts some text at a bookmark and then reapplies the
bookmark, in case you need work with the same bookmark later on.

object oBookmarkName = "BookmarkName";
Word.Range rngRange =
applicationObject.ActiveDocument.Bookmarks.get_Item(ref
oBookmarkName).Range;
rngRange.Text = "Text to be inserted and bookmarked";
object oRange = rngRange;
applicationObject.ActiveDocument.Bookmarks.Add("BookmarkName", ref oRange);

The following code simply inserts some text at a bookmark. In the process,
the bookmark will be deleted, unless it contained no text to start with, in
which case it will still exist but the inserted text will fall after the
bookmark, not within it.

object oAnotherBookmarkName = "AnotherBookmarkName";
applicationObject.ActiveDocument.Bookmarks.get_Item(ref
oAnotherBookmarkName).Range.Text = "Text to be inserted";
 
Hi Bill,

Thanks for your code.
In your example code they are using the predefined string, however in my
case, I will need to insert the text from the clipboard.

I copied the content of first Word document into clipboard:
WordDoc1.Content.Select();

Then I need to paste the clipboard into my second document:
WordDoc2.Range rngRange =
applicationObject.ActiveDocument.Bookmarks.get_Item(ref
oBookmarkName).Range;
rngRange.Text = // <------ here how do I assign the text from clipboard to
this rngRange object ?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top