View Single Post
  #6   Report Post  
Greg Maxey
 
Posts: n/a
Default I need to Delete All Bookmarks at once in a document

Doug,

I got the term "Placeholder bookmark" he
http://word.mvps.org/FAQs/MacrosVBA/...hBookmarks.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. ;-)


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Doug Robbins - Word MVP wrote:
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.
"Greg" wrote in message
oups.com...
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.