Thread: [Solved] delete all bookmark text
View Single Post
 
Old 08-09-2011, 04:22 PM
gmaxey gmaxey is offline Windows XP Office 2010 (Version 14.0)
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

I think you are going to have to do that manually. Here is why. When you insert text like your example you are insert the text "at" the bookmark. Specifically just after it. Once it is there, there is nothing to define what the "end" range is.

The better way that would give you a mean to later to delete the text is to insert text "in" a bookmark.

Sub ScratchMacro()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks("Name2").Range
oRng.Text = "anything"
ActiveDocument.Bookmarks.Add "Name2", oRng
End Sub

You can then delete the text or the text and the bookmark with:

Sub ScratchMacroII()
'A quick macro scratch pad created by Greg Maxey
Dim i As Long
Dim pStr As String
Dim oRng As Word.Range
'Delete text and preserve bookmark
For i = ActiveDocument.Bookmarks.Count To 1 Step -1
Set oRng = ActiveDocument.Bookmarks(i).Range
pStr = ActiveDocument.Bookmarks(i).Name
oRng.Text = ""
ActiveDocument.Bookmarks.Add pStr, oRng
Next i
'delete both
For i = ActiveDocument.Bookmarks.Count To 1 Step -1
ActiveDocument.Bookmarks(i).Range.Delete
Next i
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote