Skip to main content

How to select or delete text between two bookmarks in Word document?

Do you know how to select or delete the content between two bookmarks in a Word document? This article will show you methods to deal with it.

Select/delete text between two bookmarks with VBA


Select/delete text between two bookmarks with VBA

Supposing there are two bookmarks in your document as below screenshot shown, you can select or delete all contents between these two bookmarks with the following VBA code.

1. In the document, press the Alt + F11 keys to open the Microsoft Visual Basic for Applications window.

2. In the Microsoft Visual Basic for Applications window, click Insert > Module, then copy below code into the Module window.

For selecting or deleting texts between two bookmarks, please use this code:

VBA code: Select text between two specified bookmarks in Word:

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

VBA code: Delete text between two specified bookmarks in Word:

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 the F5 key to run the code.

4. In the first Kutools for Word dialog box, enter the start bookmark name into the text box and click OK.

5. In the second Kutools for Word dialog box, enter the end bookmark and click OK. See screenshot:

Note: If the bookmark name you entered does not exist in document, another Kutools for Word dialog box will pop up to remind you of entering the correct one.

Then all contents between the specified bookmarks are selected or deleted immediately. See screenshots.

Select texts between two bookmarks:

Delete texts between two bookmarks.

Best Office Productivity Tools

Kutools for Word - Elevate Your Word Experience with Over 100 Remarkable Features!

🤖 Kutools AI Assistant: Transform your writing with AI - Generate Content  /  Rewrite Text  /  Summarize Documents  /  Inquire for Information based on Document, all within Word

📘 Document Mastery: Split Pages  /  Merge Documents  /  Export Selection in Various Formats (PDF/TXT/DOC/HTML...)  /  Batch Convert to PDF  /  Export Pages as Images  /  Print Multiple Files at once...

Contents Editing: Batch Find and Replace across Multiple Files  /  Resize All Pictures  /  Transpose Table Rows and Columns  /  Convert Table to Text...

🧹 Effortless Clean: Sweap away Extra Spaces  /  Section Breaks  /  All Headers  /  Text Boxes  /  Hyperlinks  /  For more removing tools, head to our Remove Group...

Creative Inserts: Insert Thousand Separators  /  Check Boxes  /  Radio Buttons  /  QR Code  /  Barcode  /  Diagonal Line Table  /  Equation Caption  /  Image Caption  /  Table Caption  /  Multiple Pictures  /  Discover more in the Insert Group...

🔍 Precision Selections: Pinpoint specific pages  /  tables  /  shapes  /  heading paragraphs  /  Enhance navigation with more Select features...

Star Enhancements: Navigate swiftly to any location  /  auto-insert repetitive text  /  seamlessly toggle between document windows  /  11 Conversion Tools...

👉 Want to try these features? Kutools for Word offers a 60-day free trial, with no limitations! 🚀
Free Download     Read More     Buy Now
 
Comments (0)
No ratings yet. Be the first to rate!
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations