# Deleting Selected Bookmarks - MS Word



## jl123 (Mar 3, 2009)

Hi,

I'm trying to create a macro delete to delete bookmarks from word. I'm pretty new to this but I found a macro (below) that deletes all bookmarks from a page and 1 that deletes an individual bookmark. What I'm looking to do is delete certain selected ones.

ex. bookmark1
bookmark2
bookmark3
bookmark4

I would want to highlight and delete bookmark 1 and 4.

Here's what I have so far. I'm sure this just needs to be tweeked abit.

Sub Delete_Bookmarks()
Dim Selected_Bookmark As Bookmark
ActiveDocument.Bookmarks.ShowHidden = True
If ActiveDocument.Bookmarks.Count >= 1 Then
For Each Selected_Bookmark In ActiveDocument.Bookmarks
Selected_Bookmark.Delete
Next Selected_Bookmark

End If
End Sub

Here is also another that deletes a single selected bookmark. 

Sub DelBookmarks_individually()


For n = 1 To Selection.Bookmarks.Count
Selection.Bookmarks.Delete
Next n
End Sub

Any help would be appreciated. I tried to search but didn't find what I was looking for.


----------



## nirvana_ (Mar 29, 2009)

Here is the code for it.


```
Sub DeleteBookmarks()
    Dim BM As Bookmarks
    Dim Response As String
    Dim BMNumber, Cnt As Integer
    Set BM = ThisDocument.Bookmarks
    BMNumber = BM.Count
    Cnt = 1
    Do While Cnt <= BMNumber And BMNumber > 0
        BM(Cnt).Select
        Response = MsgBox("Do you want to delete bookmark: " & BM(Cnt).Name, vbYesNo)
        If Response = vbYes Then
            BM(Cnt).Delete
            BMNumber = BMNumber - 1
            Cnt = Cnt - 1
        End If
        Cnt = Cnt + 1
    Loop
End Sub
```


----------



## Macropod (Mar 30, 2009)

Here's a different approach to the same problem:

```
Sub Delete_Selected_Bookmarks()
Dim BkMrkList As String
Dim BkMrk As Bookmark
With ActiveDocument
  BkMrkList = InputBox("Please insert the bookmark names to delete, separated by spaces only")
  For Each BkMrk In .Bookmarks
    If InStr(BkMrkList, BkMrk.Name) > 0 Then BkMrk.Delete
  Next BkMrk
End With
End Sub
```
Cheers


----------



## peter1213 (Apr 1, 2009)

Hi Guys, I need help w/ this macro deleting bookmark coding.

I have (for example) a lot of bookmarks that start with "End" and change depending the bookmark from End101, End32, End55, Endtp, EndRic, etc.  I'm trying to create a macro to be able delete all the bookmarks that have "End" in it, not just the "End" bookmark but all the other ones that have either numbers or other letters.

I have this code:

Sub Macro1()
'
' Macro1 Macro
' bookmark 3 delete
'
    Selection.GoTo What:=wdGoToBookmark, Name:="End"
    ActiveDocument.Bookmarks("End*").Delete
    With ActiveDocument.Bookmarks
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With
End Sub


----------



## nirvana_ (Apr 1, 2009)

Try this.


```
Sub DeleteBookmarks()
    Dim BM As Bookmarks
    Dim j As Integer
    Set BM = ActiveDocument.Bookmarks
    If BM.Count = 0 Then
        MsgBox "No bookmarks on this document"
    End If
    For j = BM.Count To 1 Step -1
        If Left(LCase(BM(j).Name), 3) = "end" Then
            BM(j).Delete
        End If
    Next
End Sub
```


----------



## DuncanWallace79 (Jul 25, 2012)

I've got another one here.  I'm looking for a macro that removes a bookmark based on another bookmarks value.  Is this even possible?


----------



## Macropod (Jul 25, 2012)

Possibly, but you'd need to provide more details.


----------



## DuncanWallace79 (Jul 25, 2012)

I have a bookmark called [Area] and another bookmark called [Project].  Basically what I am after is when the bookmarks are substituted from fields in a database, if Project equals a certain value, can Area be removed or replaced with white space?


----------



## Macropod (Jul 25, 2012)

That sounds rather like you're doing a mailmerge, in which case you don't need a macro - mailmerge field coding can take care of it. In any event, mailmerge fields and bookmarks don't survive a mailmerge.

If it's not a mailmerge, you should consider not having the space before (or after) the bookmark and include it in whatever code you're using to update the bookmarks - which should include not populating (or de-populating) the Area bookmark if the Project bookmark has the relevant value.


----------



## DuncanWallace79 (Jul 25, 2012)

Thanks Paul,  It's custom written software that's generating reports and I was hoping to avoid hard-coding it to fix my problem.  Tweaking word templates would be much more maintainable.  I'll do some more digging but I suspect that it will have to be a hardcoded solution.


----------



## jl123 (Mar 3, 2009)

Hi,

I'm trying to create a macro delete to delete bookmarks from word. I'm pretty new to this but I found a macro (below) that deletes all bookmarks from a page and 1 that deletes an individual bookmark. What I'm looking to do is delete certain selected ones.

ex. bookmark1
bookmark2
bookmark3
bookmark4

I would want to highlight and delete bookmark 1 and 4.

Here's what I have so far. I'm sure this just needs to be tweeked abit.

Sub Delete_Bookmarks()
Dim Selected_Bookmark As Bookmark
ActiveDocument.Bookmarks.ShowHidden = True
If ActiveDocument.Bookmarks.Count >= 1 Then
For Each Selected_Bookmark In ActiveDocument.Bookmarks
Selected_Bookmark.Delete
Next Selected_Bookmark

End If
End Sub

Here is also another that deletes a single selected bookmark. 

Sub DelBookmarks_individually()


For n = 1 To Selection.Bookmarks.Count
Selection.Bookmarks.Delete
Next n
End Sub

Any help would be appreciated. I tried to search but didn't find what I was looking for.


----------



## Macropod (Jul 25, 2012)

In my experience, most 'custom written software' that uses bookmarks doesn't actually update the bookmarks - it just inserts something after them. If your's is anything like that, deleting the bookmarked range once the report has been generated isn't going to help.

You can confirm what's happening by pressing F5 in a report, choosing 'Bookmark'  then selecting your 'Area' bookmark. If all of the text that was inserted there doesn't get highlighted, the software is only inserting the text after the bookmark.


----------

