Consulting

Results 1 to 10 of 10

Thread: Unable to batch delete hidden bookmarks in Word 2016

  1. #1
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    5
    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 Wizard
    Joined
    Apr 2007
    Posts
    6,936
    Location
    Hidden bookmarks begin with an underscore _

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

    ------------------------------------------------------------------------------------------------------------------------
    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)
    (multiple files can be selected while holding Ctrl key) / 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
    5
    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
    3,963
    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
    5
    Location
    Paul, thanks for the code supplied. When I try to run either though I am getting the same error message:

    Compile error: method or data member not found. I click ok and it highlights the first line "Sub Demo()" in bright yellow and also selects the ".Left" on the If line.

    Does this mean anything to you?

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    3,963
    Location
    Sorry - the '.Left' should have been just 'Left'. Fixed.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    3
    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

  8. #8
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    5
    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

  9. #9
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    3
    Location
    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

  10. #10
    VBAX Newbie
    Joined
    Feb 2020
    Posts
    5
    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!

Posting Permissions

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