I need to Delete All Bookmarks at once in a document

D

Deane-Denver

I need to Delete All Bookmarks at once in a word document - it is not a
template
 
D

Doug Robbins - Word MVP

If you want to delete the bookmarks, but retain anything that they contain,
use:

Dim i As Long
With ActiveDocument
For i = .Bookmarks.Count To 1 Step -1
.Bookmarks(i).Delete
Next i
End With

If you want to delete the bookmarks and their contents, then use:

Dim i As Long
With ActiveDocument
For i = .Bookmarks.Count To 1 Step -1
.Bookmarks(i).Range.Delete
Next i
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
M

macropod

Of course, you might also want to preserve whatever was referring to the
bookmarks before deleting them ...

Cheers
 
G

Greg

Doug,

Run the code you posted with a couple of placeholder bookmarks and you
will see undesirable results. The character immediately to the right
of the bookmark is deleted but the bookmark stays put ;-(

Sub Test()
Dim i As Long
With ActiveDocument
For i = .Bookmarks.Count To 1 Step -1
.Bookmarks(i).Range.Delete
Next i
End With
End Sub

Now consider this:
Sub DeleteAllBookmarksRefined()
Dim i As Long
With ActiveDocument
For i = .Bookmarks.Count To 1 Step -1
If Len(.Bookmarks(i).Range) <> 0 Then
.Bookmarks(i).Range.Delete
Else
.Bookmarks(i).Delete
End If
Next i
End With
End Sub

Cheers.
 
D

Doug Robbins - Word MVP

Hi Greg,

Your version of "placeholder bookmarks" do not have content and the second
macro was for use if you wanted to delete both the bookmarks AND their
contents.

While as far as I can tell, there is no term in Word that equates to a
"placeholder bookmark", my version of it would always include a single space
so that if I used .InsertBefore on the .Range of the bookmark, the text
would be inserted inside the bookmark and could thus be cross-referenced.
 
G

Greg Maxey

Doug,

I got the term "Placeholder bookmark" here:
http://word.mvps.org/FAQs/MacrosVBA/WorkWithBookmarks.htm

(1) Placeholder Bookmarks
If you click somewhere in the document and insert a bookmark it will look
like a beam I – this is a “placeholder” bookmark.

If a document has one of those kind and you run this code:

Sub Test()
Dim i As Long
With ActiveDocument
For i = .Bookmarks.Count To 1 Step -1
.Bookmarks(i).Range.Delete
Next i
End With
End Sub

It will delete the character following the bookmark and leave the bookmark
intact (i.e, it doesn't delete the bookmark). I am not trying to justify
what is or is not the correct way to use bookmarks, merely pointing out that
running the above code in a document that does contain a "placeholder
bookmark" will have undesirable results. ;-)
 
D

Doug Robbins - Word MVP

Hi Greg,

That makes it an Ibby term. I searched for it in the Word Help file and did
not come up with it.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
G

Greg Maxey

Hi Doug,

Ok, forget Ibby. Do you concede that it is humanly possible for a person,
not yourself of course, to create a document that has a dispersion of
bookmarks some enclosing text and some with a zero length range and that
these bookmarks of the second type have at least, by a small segment of the
population, been referred to as "placeholder bookmarks?"

This person then makes the following request:

<I need to Delete All Bookmarks at once in a word document - it is not a
template
A very knowledgeable person replies.

If you want to delete the bookmarks and their contents, then use:


Dim i As Long
With ActiveDocument
For i = .Bookmarks.Count To 1 Step -1
.Bookmarks(i).Range.Delete
Next i
End With

The person ponders and decides yes I do want to delete the bookmarks and
their contents. He or she executes the code and thousands of bookmarks
vanish in the blink of an eye. File>Save quit Word and off to catch the
morning train.

Can you imagine the dismay when that person realizes later that many of the
bookmarks, the placeholder type, are still present and not only that but a
single adjacent character has been deleted instead?

Cheers
 
Top