Consulting

Results 1 to 9 of 9

Thread: Unable to batch delete hidden bookmarks in Word 2016

  1. #1
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    4
    Location

    Unable to batch delete hidden bookmarks in Word 2016

    Version of the program: Word 2016
    What you want it to do: I have a document which has quite a lot of bookmarks in it (both hidden and not), it seems like the user has removed its TOC but now also wants to remove the remaining "TOC and HIK" hidden bookmarks within the document. I have managed to find a macro which deletes any TOC and HIK bookmarks which are not hidden but for the life of me cannot figure out how to tell the macro to look for and delete the hidden bookmarks mentioned.
    This is what I have been using which does delete non hidden bookmarks:

    Dim aBookmark
    For Each aBookmark In ActiveDocument.Bookmarks
    If Left(aBookmark.Name, 3) = "TOC" Then
    aBookmark.Delete
    ElseIf Left(aBookmark.Name, 3) = "HIK" Then
    aBookmark.Delete
    End If
    Next
    End Sub

    Error messages if any: None

    Please note, this is my first time actually working with macros at all so please excuse my ignorance.

    Thanks

    Chris

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Hidden bookmarks begin with an underscore _

    Try adding 2 more ElseIf's for "_TOC" and "_HIK"
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    4
    Location
    Quote Originally Posted by Paul_Hossler View Post
    Hidden bookmarks begin with an underscore _

    Try adding 2 more ElseIf's for "_TOC" and "_HIK"
    Hi Paul, thanks for the reply. I had also tried that earlier but they dont seem to get deleted?

    I also saw another line of code on the web to try to "show" the hidden ones first before attempting to delete them but that did not work. That code was:

    ActiveDocument.Bookmarks.ShowHidden = True
    I also tried using an asterisk as a wildcard in the code (which is probably not even correct) as I thought the numbers after the named _TOC bookmarks were causing an issue but I dont think a wildcard is actually needed as I just created a standard bookmark called TOC8762862 and then ran the macro and that removed that bookmark just fine.

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    At its simplest:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim i As Long
    With ActiveDocument
      For i = .Bookmarks.Count To 1 Step -1
        With .Bookmarks(i)
          If Left(.Name, 1) = "_" Then .Delete
        End With
      Next
    End With
    Application.ScreenUpdating = True
    End Sub
    Alternatively:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim i As Long
    With ActiveDocument
      For i = .Bookmarks.Count To 1 Step -1
        With .Bookmarks(i)
          Select Case Left(.Name, 4)
            Case "_TOC", "_HIK": .Delete
          End Select
        End With
      Next
    End With
    Application.ScreenUpdating = True
    End Sub
    Last edited by macropod; 02-06-2020 at 05:01 AM.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    4
    Location
    Try it:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim i As Long
      For i = ActiveDocument.Bookmarks.Count To 1 Step -1
          Select Case Left(ActiveDocument.Bookmarks(i).Name, 4)
            Case "_TOC", "_HIK", "RESU": ActiveDocument.Bookmarks(i).Delete
          End Select
      Next
    Application.ScreenUpdating = True
    End Sub
    Last edited by Aussiebear; 08-30-2022 at 04:02 PM. Reason: Added code tags to supplied code

  6. #6
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    4
    Location
    Quote Originally Posted by Emby View Post
    Try it:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim i As Long
    For i = ActiveDocument.Bookmarks.Count To 1 Step -1
    Select Case Left(ActiveDocument.Bookmarks(i).Name, 4)
    Case "_TOC", "_HIK", "RESU": ActiveDocument.Bookmarks(i).Delete
    End Select
    Next
    Application.ScreenUpdating = True
    End Sub
    Thanks both... I have run both of these options but when I'm going to bookmark another piece of text (to check if the other bookmarks remain) then it still shows them all If I select one of the _TOC bookmarks and click GO-TO then it still hops around the document to the bookmarks in question.

    hidden bookmarks.JPG

  7. #7
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    4
    Location
    Please add "Ucase" to convert "_Toc" into "_TOC".

    Sub Demo()
    Application.ScreenUpdating = False
    Dim i As Long
    For i = ActiveDocument.Bookmarks.Count To 1 Step -1
    Select Case Ucase(Left(ActiveDocument.Bookmarks(i).Name, 4))
    Case "_TOC", "_HIK": ActiveDocument.Bookmarks(i).Delete
    End Select
    Next
    Application.ScreenUpdating = True
    End Sub
    Last edited by Aussiebear; 08-30-2022 at 04:02 PM. Reason: Added code tags to supplied code

  8. #8
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    4
    Location

    Wink

    Quote Originally Posted by Emby View Post
    Please add "Ucase" to convert "_Toc" into "_TOC".

    ub Demo()
    Application.ScreenUpdating = False
    Dim i As Long
    For i = ActiveDocument.Bookmarks.Count To 1 Step -1
    Select Case Ucase(Left(ActiveDocument.Bookmarks(i).Name, 4))
    Case "_TOC", "_HIK": ActiveDocument.Bookmarks(i).Delete
    End Select
    Next
    Application.ScreenUpdating = True
    End Sub
    I would like to thank you both for helping with this. I can understand how tiring it must be sometimes helping people who have no knowledge of the coding area of IT but you have really been super helpful and patient in this instance.

    Once again thank you!

  9. #9
    Just to say you need to set it to show hidden bookmarks.

    I've also added a message showing the number deleted. May help others who find this via google search. I edited it to only delete _TOC... bookmarks because I can't find documentation about _HIK and there weren't any in my document.


    Sub Demo()
    Application.ScreenUpdating = False
    ActiveDocument.Bookmarks.ShowHidden = True
    Dim i As Long
    Dim els As Long
    For i = ActiveDocument.Bookmarks.Count To 1 Step -1
    Select Case UCase(Left(ActiveDocument.Bookmarks(i).Name, 4))
    Case "_TOC":
    els = els + 1
    ActiveDocument.Bookmarks(i).Delete
    End Select
    Next
    MsgBox ("_Toc bookmarks deleted: " + CStr(els))
    Application.ScreenUpdating = True
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •