How to Select or Delete Text Between Two Bookmarks in Word?

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:

How to Select or Delete Text Between Two Bookmarks in Word

How to Select or Delete Text Between Two Bookmarks in Word

  1. In the document, press Alt + F11 to open the Microsoft Visual Basic for Applications window.
  2. 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 String

    On 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 If

    xRange.Start = xBMone.Range.End
    xRange.End = xBMtwo.Range.Start
    xRange.Select
    End Sub

    Sub DeleteBetweenBookmarks()
    Dim xRange As Range
    Dim xBMone, xBMtwo As Bookmark
    Dim xBookMarkOne, xBookMarkTwo As String

    On 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 If

    xRange.Start = xBMone.Range.End
    xRange.End = xBMtwo.Range.Start
    xRange.Delete
    End Sub

  3. Press F5 to run the code.
  4. In the first Kutools for Word dialog, enter the starting bookmark name in the text box and click OK.
  5. 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.

 

We will be happy to hear your thoughts

Leave a reply

Gotkey.net
Logo
Compare items
  • Total (0)
Compare
0
Shopping cart