Do you know how to select or delete content between two bookmarks in a Word document? This article will guide you through the methods to achieve this.
Select/Delete Text Between Two Bookmarks Using VBA
Suppose you have two bookmarks in your document as shown in the screenshot below, and you want to select or delete all content between these two bookmarks using the following VBA code:
- In the document, press Alt + F11 to open the Microsoft Visual Basic for Applications window.
- In the Microsoft Visual Basic for Applications window, click Insert > Module, then copy and paste the code below into the Module window:
Sub SelectBetweenBookmarks()
Dim xRange As Range
Dim xBMone, xBMtwo As Bookmark
Dim xBookMarkOne, xBookMarkTwo As StringOn Error Resume Next
Set xRange = ActiveDocument.Content
xBookMarkOne = InputBox(“Please enter the start bookmark:”, “Kutools for Word”)
xBookMarkTwo = InputBox(“Please enter the end bookmark:”, “Kutools for Word”)
Set xBMone = ActiveDocument.Bookmarks(xBookMarkOne)
Set xBMtwo = ActiveDocument.Bookmarks(xBookMarkTwo)If xBMone Is Nothing Or xBMtwo Is Nothing Then
MsgBox “Please enter the correct bookmark name”, vbInformation, “KuTools for Word”
Exit Sub
End IfxRange.Start = xBMone.Range.End
xRange.End = xBMtwo.Range.Start
xRange.Select
End SubSub DeleteBetweenBookmarks()
Dim xRange As Range
Dim xBMone, xBMtwo As Bookmark
Dim xBookMarkOne, xBookMarkTwo As StringOn Error Resume Next
Set xRange = ActiveDocument.Content
xBookMarkOne = InputBox(“Please enter the start bookmark:”, “Kutools for Word”)
xBookMarkTwo = InputBox(“Please enter the end bookmark:”, “Kutools for Word”)
Set xBMone = ActiveDocument.Bookmarks(xBookMarkOne)
Set xBMtwo = ActiveDocument.Bookmarks(xBookMarkTwo)If xBMone Is Nothing Or xBMtwo Is Nothing Then
MsgBox “Please enter the correct bookmark name”, vbInformation, “KuTools for Word”
Exit Sub
End IfxRange.Start = xBMone.Range.End
xRange.End = xBMtwo.Range.Start
xRange.Delete
End Sub - Press F5 to run the code.
- In the first Kutools for Word dialog, enter the starting bookmark name in the text box and click OK.
- In the second Kutools for Word dialog, enter the ending bookmark name and click OK.
If the bookmark names you entered do not exist in the document, another Kutools for Word dialog will prompt you to enter the correct names.
After that, all content between the specified bookmarks will be selected or deleted immediately.
Select texts between two bookmarks:
Delete texts between two bookmarks.