none
Change the “name” of a bookmark not the text of it RRS feed

  • Question

    Thanks to the guys in the ASP.NET forums who redirected me here.. I am searching for almost a week now but can’t find the answer for this. I need to change the “name” of the bookmark itself rather than the text it holds. The name property of the bookmark is read-only as you know : ( I am using a code like this:

     

    using Word = Microsoft.Office.Interop.Word; 
     
    Word.Application wdApp = new Word.Application(); 
    Word.Document wdDoc = new Word.Document(); 
     
    object fileName = @"C:\test.docx"; 
    wdDoc = wdApp.Documents.Open(ref fileName); 
    wdApp.Visible = false; 
     
    Word.Range rng = wdDoc.Bookmarks["bookmarkName"].Range; 
    rng.Text = "this is changes the text held by a bookmark; it isn’t want I need"; 
    rng.Name  ???
    

     

    I would appreciate if you could give me a hint on how to access a bookmark name/text using Open XML as well (only if you have some extra free time for this one : )) I wonder if this would be an easier solution than Word Automation!

    Many thanks in advnace for your assistance : )




    • Edited by Lion103 Saturday, October 15, 2011 2:28 PM
    • Moved by Cindy Meister MVPModerator Saturday, October 15, 2011 5:24 PM Word object model specific (From:Visual Studio Tools for Office)
    Saturday, October 15, 2011 2:26 PM

Answers

  • Hi Lion

    as you're not using the VSTO technology I'm going to move your question to the more appropriate Word for Developers forum. But...

    You can't change the name of a bookmark. You have to create a new bookmark (which can target the same range as an existing bookmark) and then delete the bookmark with the "bad" name if you no longer want it. So something like this, based on your code:

    Word.Bookmark bkmNew = wdDoc.Bookmarks.Add("newname", rng);

    However, it appears that what you really want to do is recreate the bookmark around the new text, as assigning text to a bookmark's range does delete the bookmark. Again, the above code snippet will work, although in that case you don't necessarily need to instantiate a new bookmark object, as I did above.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Lion103 Sunday, October 16, 2011 6:33 AM
    Saturday, October 15, 2011 5:24 PM
    Moderator
  • There isn't a rename function. You have to delete the old name and mark the range with a new bookmark name. Here's sample VBA code:

    Sub ReNameBookMark()
        Dim doc As Word.Document
        Dim rng As Word.Range
        Dim bmk As Word.Bookmark
        
        Set doc = Word.ActiveDocument
        Set rng = doc.Bookmarks("MyMark").Range
        Set bmk = doc.Bookmarks("MyMark")
        
        Debug.Print rng.Text
        Debug.Print bmk.Name
        bmk.Delete
        rng.Bookmarks.Add ("YourName")
        Debug.Print rng.Bookmarks.Item(1).Name
    End Sub
    
    

    I can't help you with your Open XML question.


    Kind Regards, Rich ... http://greatcirclelearning.com
    • Marked as answer by Lion103 Sunday, October 16, 2011 6:33 AM
    Saturday, October 15, 2011 5:32 PM

All replies

  • Hi Lion

    as you're not using the VSTO technology I'm going to move your question to the more appropriate Word for Developers forum. But...

    You can't change the name of a bookmark. You have to create a new bookmark (which can target the same range as an existing bookmark) and then delete the bookmark with the "bad" name if you no longer want it. So something like this, based on your code:

    Word.Bookmark bkmNew = wdDoc.Bookmarks.Add("newname", rng);

    However, it appears that what you really want to do is recreate the bookmark around the new text, as assigning text to a bookmark's range does delete the bookmark. Again, the above code snippet will work, although in that case you don't necessarily need to instantiate a new bookmark object, as I did above.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Lion103 Sunday, October 16, 2011 6:33 AM
    Saturday, October 15, 2011 5:24 PM
    Moderator
  • There isn't a rename function. You have to delete the old name and mark the range with a new bookmark name. Here's sample VBA code:

    Sub ReNameBookMark()
        Dim doc As Word.Document
        Dim rng As Word.Range
        Dim bmk As Word.Bookmark
        
        Set doc = Word.ActiveDocument
        Set rng = doc.Bookmarks("MyMark").Range
        Set bmk = doc.Bookmarks("MyMark")
        
        Debug.Print rng.Text
        Debug.Print bmk.Name
        bmk.Delete
        rng.Bookmarks.Add ("YourName")
        Debug.Print rng.Bookmarks.Item(1).Name
    End Sub
    
    

    I can't help you with your Open XML question.


    Kind Regards, Rich ... http://greatcirclelearning.com
    • Marked as answer by Lion103 Sunday, October 16, 2011 6:33 AM
    Saturday, October 15, 2011 5:32 PM
  • Many thanks for you Cindy & Rich, your answers solved my problem.

    I will mark both as answers.

    Cheers

    Sunday, October 16, 2011 6:33 AM
  • Hi Lion

    Glad our suggestions worked for you :-) I missed the Open XML part of your original question. So, for the sake of completeness...

    <<I would appreciate if you could give me a hint on how to access a bookmark name/text using Open XML as well (only if you have some extra free time for this one : )) I wonder if this would be an easier solution than Word Automation!>>

    Whether Open XML is a valid approach depends very much on exactly what task you have to perform and which file formats you're targeting. Open XML only works for the file formats introduced with Office 2007 (but older versions of Word can open and work with these).

    If what you're basically doing is creating new documents from a "template", filling in values, then passing them on to a user then, yes, Open XML would be a valid and probably desirable approach.


    Cindy Meister, VSTO/Word MVP
    Sunday, October 16, 2011 3:29 PM
    Moderator