Deleting Selected Bookmarks - MS Word

jl123

New Member
Joined
Mar 3, 2009
Messages
2
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(n).Delete
Next n
End Sub

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

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Here is the code for it.

Code:
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
 
Upvote 0
Here's a different approach to the same problem:
Code:
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
 
Upvote 0
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
 
Upvote 0
Try this.

Code:
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
 
Upvote 0
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?
 
Upvote 0
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?
 
Upvote 0
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.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,216,991
Messages
6,133,902
Members
449,845
Latest member
Lakem

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top