VBA - Delete words not Bookmarks

J

jamiee

Okay, I have some code which is deleting text at 6 different bookmark
points. I want to just delete the text, and not the bookmarks. For
some reason the first text at the first bookmark along with it's
bookmark is deleted when the code is executed, however the remaining
bookmarks remains. The code is exactly the same for each bookmark so I
don't understand why the first bookmark is deleted but the others
remain. Does anyone have any ideas why this is?

If I set the count value for the first deletion to 3, 3 characters are
deleted but the bookmark is deleted as well, even though some of the
text remains. Does anyone know what has gone wrong? or better still
how to correct this?

Private Sub Delete_Address()
With ActiveDocument
.Bookmarks("StdAdd1").Select
Selection.Delete Unit:=wdCharacter, Count:=6
.Bookmarks("StdAdd2").Select
Selection.Delete Unit:=wdCharacter, Count:=6
.Bookmarks("StdAdd3").Select
Selection.Delete Unit:=wdCharacter, Count:=7
.Bookmarks("StdAdd4").Select
Selection.Delete Unit:=wdCharacter, Count:=26
.Bookmarks("StdAdd5").Select
Selection.Delete Unit:=wdCharacter, Count:=24
.Bookmarks("StdAdd6").Select
Selection.Delete Unit:=wdCharacter, Count:=18
End With
End Sub
 
M

mbaird

When you delete all of the data in a bookmark the bookmark will be deleted.
You will need to replace the data with a space. Do you want to preserve the
bookmark so that you can insert data back into the bookmark?

The following code does that.

Dim oRange As Range
Set oRange = ActiveDocument.Bookmarks("test").Range
' Insert a space before the text.
oRange.Characters(1).InsertBefore " "
' Redefine the range to not include the space.
oRange.SetRange oRange.Start + 1, oRange.End
' Delete the text.
oRange.Delete


Mark Baird
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top