none
Bookmarks into word RRS feed

  • Question

  • Hello,

    I am trying to insert from within c# into word, but I just started out with this. I have seen a lot of examples the last couple of hours. Some examples give me some errors.

    I saw this piece of code:

    Word._Application oWordDoc;
                Word._Document oDoc;
                oWordDoc = new Word.Application();
                oWordDoc.Visible = true;
                oDoc = oWordDoc.Documents.Add(ref oMissing, ref oMissing,
                    ref oMissing, ref oMissing);
                Object oBookmarkStart = "BookMark__Start";
                Object oRngoBookMarkStart = oWordDoc.Bookmarks.get_Item(ref oBookmarkDesignInfoStart).Range.Start;

    The above piece is working, but when it gets to the last line:

    oWordDoc.Bookmarks.get... etc

    It gives me the following error:

    Error 1 'Microsoft.Office.Interop.Word._Application' does not contain a definition for 'Bookmarks' and no extension method 'Bookmarks' accepting a first argument of type 'Microsoft.Office.Interop.Word._Application' could be found (are you missing a using directive or an assembly reference?) 

    I just copied this code from online, but my intention is the following:

    I want to create a table and put addressess into is. Therefore I could use some codebehind table generating, but I get on that errror also. Or I could use a template word document. For a little more progressive way I want first to put it into a template word document.

    Any help?

    Greeting,

    Spacelama

    • Moved by Bob Shen Friday, May 4, 2012 6:38 AM (From:Visual C# General)
    Wednesday, May 2, 2012 6:11 PM

Answers

  • 0bject oBookmarkDesignInfoStart = new object();
               
                Object oBookmarkStart = "BookMark__Start";
                Object oRngoBookMarkStart = oDoc.Bookmarks.get_Item(ref oBookmarkDesignInfoStart).Range.Start;

    So I am wondering how does this piece of code find the bookmark in word and fills it with a string? Also what happens if I have multiple bookmarks with the same naming?

    Hi SpaceLama

    In your sample code, in order to make things clearer for you, you might do this:

      Word.Bookmark bkm = oDoc.Bookmarks.get_Item(ref oBookmarkDesignInfoStart);

    Now you have a Bookmark object and if you type bkm. you'll get the Intellisense list of properties and methods for a Bookmark object.

    In order to write text to a bookmark, you want the Range.Text property so:

      bkm.Range.Text = "text for the bookmark";

    As fumei mentions, bookmark names in a document must be unique.

    Word has two kinds of bookmark. One is a "marker" and can be visualized like a cursor I-Beam (it looks like "I"). This kind of bookmark does not contain content, but only marks a position. If you write information to this kind of bookmark, the bookmark is not deleted from the document.

    The other kind of bookmark can be visualized like this: [bookmark content], where [ ] represent the Start and End points of the bookmark range. When you write text to this kind of bookmark, the bookmark is removed from the document. If you need to retain the bookmark, you should use an approach such as fumei suggests with the sample VBA code.

    Depending on the version of Word you want to support, an alternative to bookmarks would be Content Controls.

    Please also note that, if you need to process documents that aren't being opened and edited by a user that you can work with them independent of the Word application by using the Open XML SDK. This is especially important if your application needs to run in a server environment.


    Cindy Meister, VSTO/Word MVP

    Saturday, May 5, 2012 7:09 AM
    Moderator

All replies

  • I think that Bookmarks is a property of document, not of application. Therefore instead of oWordDoc.Bookmarks the next should work: oDoc.Bookmarks.

    Wednesday, May 2, 2012 6:35 PM
  • Thanks,

    0bject oBookmarkDesignInfoStart = new object();
                
                Object oBookmarkStart = "BookMark__Start";
                Object oRngoBookMarkStart = oDoc.Bookmarks.get_Item(ref oBookmarkDesignInfoStart).Range.Start;

    So I am wondering how does this piece of code find the bookmark in word and fills it with a string? Also what happens if I have multiple bookmarks with the same naming?

    Greetings,

    Spacelama

    Wednesday, May 2, 2012 7:03 PM
  • Also what happens if I have multiple bookmarks with the same naming?

    Not possible.  Word allows only unique names for bookmarks.  If you give a different location the same name as an existing bookmark, the previously existing bookmark is deleted.  That is, the bookmark itself, not the contents in that bookmark.

    Also note that if you put text at a boomark, like so:

    ActiveDocument.Bookmarks("myname").Range.Text = SomeString

    Then the bookmark is ALSO deleted.  The text inserted REPLACES the bookmark.

    To retain the existence of a bookmark, but still insert text into its range, you need a Sub like this:

    Sub UpdateBM(strBM As String, strText As String)
    Dim r As Range
        Set r = ActiveDocument.Bookmarks(strBM).Range
        r.Text = strText
        ActiveDocument.Bookmarks.Add Name:=strBM, Range:=r
    End Sub

    Then make a Call to it with the bookmark name, and the text/string you want to put inside it.  You will have to adjust the code to fit C#.


    Friday, May 4, 2012 9:57 PM
  • 0bject oBookmarkDesignInfoStart = new object();
               
                Object oBookmarkStart = "BookMark__Start";
                Object oRngoBookMarkStart = oDoc.Bookmarks.get_Item(ref oBookmarkDesignInfoStart).Range.Start;

    So I am wondering how does this piece of code find the bookmark in word and fills it with a string? Also what happens if I have multiple bookmarks with the same naming?

    Hi SpaceLama

    In your sample code, in order to make things clearer for you, you might do this:

      Word.Bookmark bkm = oDoc.Bookmarks.get_Item(ref oBookmarkDesignInfoStart);

    Now you have a Bookmark object and if you type bkm. you'll get the Intellisense list of properties and methods for a Bookmark object.

    In order to write text to a bookmark, you want the Range.Text property so:

      bkm.Range.Text = "text for the bookmark";

    As fumei mentions, bookmark names in a document must be unique.

    Word has two kinds of bookmark. One is a "marker" and can be visualized like a cursor I-Beam (it looks like "I"). This kind of bookmark does not contain content, but only marks a position. If you write information to this kind of bookmark, the bookmark is not deleted from the document.

    The other kind of bookmark can be visualized like this: [bookmark content], where [ ] represent the Start and End points of the bookmark range. When you write text to this kind of bookmark, the bookmark is removed from the document. If you need to retain the bookmark, you should use an approach such as fumei suggests with the sample VBA code.

    Depending on the version of Word you want to support, an alternative to bookmarks would be Content Controls.

    Please also note that, if you need to process documents that aren't being opened and edited by a user that you can work with them independent of the Word application by using the Open XML SDK. This is especially important if your application needs to run in a server environment.


    Cindy Meister, VSTO/Word MVP

    Saturday, May 5, 2012 7:09 AM
    Moderator