Search code examples
c#ms-wordoffice-interop

How to add a Bookmark control to a Word document at runtime


I want to add in a C# application a bookmark to a particular range to my Word document at runtime . I have found one solution Microsoft.Office.Tools.Word.Bookmark bookmark1; bookmark1 = this.Controls.AddBookmark(this.Paragraphs[1].Range, "bookmark1");

But an error shows that the Windows form has no definition for AddBookmark. Please help.


Solution

  • You are using a WinForms application, therefore by using the keyword this it will refer to the form class you are using which does not have a definition for Controls.AddBookmark.

    I suggest you take a look here: http://msdn.microsoft.com/en-us/library/cc442946.aspx

    This will show you how to create a word addin from which you can then use this code to add a bookmark.

    Microsoft.Office.Tools.Word.Bookmark bookmark1; bookmark1 = this.Controls.AddBookmark(this.Paragraphs[1].Range, "bookmark1");