Log in

View Full Version : [SOLVED:] Unable to batch delete hidden bookmarks in Word 2016



VBnoob
02-05-2020, 07:19 AM
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

Paul_Hossler
02-05-2020, 08:48 AM
Hidden bookmarks begin with an underscore _

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

VBnoob
02-05-2020, 09:43 AM
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.

macropod
02-05-2020, 03:30 PM
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

Emby
02-06-2020, 05:04 AM
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

VBnoob
02-06-2020, 05:19 AM
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.

25935

Emby
02-06-2020, 06:32 AM
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

VBnoob
02-06-2020, 08:10 AM
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!

RobertInv
08-30-2022, 06:08 AM
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