none
Deleting empty bookmarks RRS feed

  • Question

  • Hi;

    In some cases we insert stuff into a Word document where everything is surrounded by a bookmark. The user will then sometimes delete the objects inserted, but not the bookmark because it is hidden and they don't know it exists.

    Any suggestions on how to delete any cases of a bookmark that is immediately followed by an end bookmark where the name matches our signature (they all start with _udt)? What would be perfect is if there was an event when text is deleted with a range so we could look 1 before/after. But that does not exist.

    Doing it on a save is problematic because we've found users insert one of our objects, delete it, then a different one (finding the one they want). By the time they save we have <w:bookmarkStart w:id="0"><w:bookmarkStart w:id="1"><w:bookmarkStart w:id="2"><w:r>...</w:r><w:bookmarkEnd w:id="0"/><w:bookmarkEnd w:id="1"/><w:bookmarkEnd w:id="2"/>

    ??? - thanks - dave


    Very funny video - What's your Metaphor?
    Monday, January 24, 2011 11:36 PM

Answers

  • Hi David,

    Here's how you could do it with a macro:

    Sub KillEmptyBkMrks()
    Dim oBkMk As Bookmark
    With ActiveDocument
      .Bookmarks.ShowHidden = True
        For Each oBkMk In .Bookmarks
          If .Bookmarks(oBkMk).Empty = True Then oBkMk.Delete
        Next
      .Bookmarks.ShowHidden = False
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]
    • Marked as answer by DavidThi808 Friday, February 18, 2011 9:18 PM
    Monday, January 24, 2011 11:47 PM
  • Hi David

    Your request is a bit baffling, as there's no such thing as an "empty bookmark". If all the content is deleted, then so is the bookmark - a behavior that's frustrated Word developers for years. The only kind of bookmark that doesn't have this behavior is the "I-beam"  bookmark, that never had content to begin with...


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by DavidThi808 Friday, February 18, 2011 9:18 PM
    Tuesday, January 25, 2011 7:49 AM
    Moderator

All replies

  • Hi David,

    Here's how you could do it with a macro:

    Sub KillEmptyBkMrks()
    Dim oBkMk As Bookmark
    With ActiveDocument
      .Bookmarks.ShowHidden = True
        For Each oBkMk In .Bookmarks
          If .Bookmarks(oBkMk).Empty = True Then oBkMk.Delete
        Next
      .Bookmarks.ShowHidden = False
    End With
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]
    • Marked as answer by DavidThi808 Friday, February 18, 2011 9:18 PM
    Monday, January 24, 2011 11:47 PM
  • Hi;

    Thank you for how to clear them out.

    But the bigger problem is when do I clear them out. Is there any kind of event when objects are deleted?

    thanks - dave


    Very funny video - What's your Metaphor?
    Tuesday, January 25, 2011 3:48 AM
  • Hi David,

    There is no general deletion event to trap. With Word 2007 & later, if you associate the objects with particular content controls, there is also a 'ContentControlBeforeDelete' event you could use.

    Alternatively, you could use the Document_Close event to fire off the code to delete the empty bookmarks whenever the document is closed.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Tuesday, January 25, 2011 5:40 AM
  • Hi David

    Your request is a bit baffling, as there's no such thing as an "empty bookmark". If all the content is deleted, then so is the bookmark - a behavior that's frustrated Word developers for years. The only kind of bookmark that doesn't have this behavior is the "I-beam"  bookmark, that never had content to begin with...


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by DavidThi808 Friday, February 18, 2011 9:18 PM
    Tuesday, January 25, 2011 7:49 AM
    Moderator
  • Hi Cindy,

    In my experience, Word can accumulate many empty bookmarks of its own accord. Here's a quick way to create some:

    1. Create a new document with three Heading-level paragraphs.
    2. Insert a TOC to pick up the paragraphs.
    3. Go to Insert|Bookmark and check the 'Hidden bookmarks' option (uncheck it first if its already checked). You should see the three bookmarks.
    4. Insert some cross-references to the headings
    5. Go to Insert|Bookmark and you should now see six! bookmarks - Word has just created another three instead of re-using the existing ones.
    6. Delete the TOC
    7. Go to Insert|Bookmark and you should now see the same six! bookmarks.
    6. Reinsert the TOC
    7. Go to Insert|Bookmark and you should now see nine! bookmarks - Word has just created another three instead of re-using the existing ones.
    8. Delete the TOC and the cross-references
    9. Go to Insert|Bookmark and you should now see the same nine! bookmarks.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Tuesday, January 25, 2011 8:50 PM
  • Hi Paul

    Yes, but they aren't empty...? The text which they surrounded is still there, and the bookmarks still surround that text. If you delete the text, the bookmarks disappear.


    Cindy Meister, VSTO/Word MVP
    Wednesday, January 26, 2011 6:18 AM
    Moderator
  • Hi Cindy,

    Well, if you run the macro I posted in a document with such 'orphaned' bookmarks, they get deleted. That suggests to me they are indeed empty.


    Cheers
    Paul Edstein
    [MS MVP - Word]
    Wednesday, January 26, 2011 8:39 AM