Delete text within two bookmarks

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Assuming I have the following text:

Code:
Hello,

"BOOKMARK_START"

What are you doing

'BOOKMARK_END"

Call me


I want to delete the text within two bookmarks in Word to end up with:

Code:
Hello,

Call me


I found this code:

Code:
Dim pRange
Dim dRange
Dim myrange

pRange = ActiveDocument.Bookmarks("BOOKMARK_START").Range.Start
dRange = ActiveDocument.Bookmarks("BOOKMARK_END").Range.End
Set myrange = ActiveDocument.Range(Start:=pRange, End:=dRange)
myrange.Cut

What happens when the code is run is BOOKMARK_START and BOOKMARK_END point to the same place, (which is BOOKMARK_START), ie:

Code:
Hello,

BOOKMARK_START / BOOKMARK_END

Call me

Next time I insert something into BOOKMARK_START (and then try to delete it), it won't recognise BOOKARK_END (because it's the same location as BOOKMARK_START).

How can I get round this?

Thanks
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
You need to create the same bookmarks right after destroying them. The sample implementation could be the following for deleting or replacing the content. (By the way, if you create the bookmarks before and after the content to be deleted, then recreating bookmarks might not be necessary. However, this is also a good approach.

VBA Code:
Sub DeleteContent()
Dim pRange
Dim dRange
Dim myrange

pRange = ActiveDocument.Bookmarks("BOOKMARK_START").Range.Start
dRange = ActiveDocument.Bookmarks("BOOKMARK_END").Range.End
Set myrange = ActiveDocument.Range(Start:=pRange, End:=dRange)
myrange.Cut

With ActiveDocument.Bookmarks
    .Add Range:=ActiveDocument.Range(myrange.Start, myrange.Start), Name:="BOOKMARK_START"
    .Add Range:=ActiveDocument.Range(myrange.End, myrange.End), Name:="BOOKMARK_END"
End With

End Sub

Sub ChangeContent()
Dim pRange As Long
Dim dRange As Long
Dim myrange As Range

pRange = ActiveDocument.Bookmarks("BOOKMARK_START").Range.Start
dRange = ActiveDocument.Bookmarks("BOOKMARK_END").Range.End
Set myrange = ActiveDocument.Range(Start:=pRange, End:=dRange)
myrange.Text = "test"

With ActiveDocument.Bookmarks
    .Add Range:=ActiveDocument.Range(myrange.Start, myrange.Start), Name:="BOOKMARK_START"
    .Add Range:=ActiveDocument.Range(myrange.End, myrange.End), Name:="BOOKMARK_END"
End With

End Sub
 
Upvote 0
You need to create the same bookmarks right after destroying them. The sample implementation could be the following for deleting or replacing the content. (By the way, if you create the bookmarks before and after the content to be deleted, then recreating bookmarks might not be necessary. However, this is also a good approach.

VBA Code:
Sub DeleteContent()
Dim pRange
Dim dRange
Dim myrange

pRange = ActiveDocument.Bookmarks("BOOKMARK_START").Range.Start
dRange = ActiveDocument.Bookmarks("BOOKMARK_END").Range.End
Set myrange = ActiveDocument.Range(Start:=pRange, End:=dRange)
myrange.Cut

With ActiveDocument.Bookmarks
    .Add Range:=ActiveDocument.Range(myrange.Start, myrange.Start), Name:="BOOKMARK_START"
    .Add Range:=ActiveDocument.Range(myrange.End, myrange.End), Name:="BOOKMARK_END"
End With

End Sub

Sub ChangeContent()
Dim pRange As Long
Dim dRange As Long
Dim myrange As Range

pRange = ActiveDocument.Bookmarks("BOOKMARK_START").Range.Start
dRange = ActiveDocument.Bookmarks("BOOKMARK_END").Range.End
Set myrange = ActiveDocument.Range(Start:=pRange, End:=dRange)
myrange.Text = "test"

With ActiveDocument.Bookmarks
    .Add Range:=ActiveDocument.Range(myrange.Start, myrange.Start), Name:="BOOKMARK_START"
    .Add Range:=ActiveDocument.Range(myrange.End, myrange.End), Name:="BOOKMARK_END"
End With

End Sub
Thanks, I'll try that.
 
Upvote 0

Forum statistics

Threads
1,215,343
Messages
6,124,394
Members
449,155
Latest member
ravioli44

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