VBA Word Macro to Select All Text in a Word Document that lies between two Bookmarks

FaheemAhmadGul
FaheemAhmadGul used Ask the Experts™
on
In a Word Document I have two Bookmarks named StartCopy and EndCopy. Between these two Bookmarks lies some text.
I need help with a word Macro that will Select all the text that lies between these two Bookmarks.
Thank you for your help.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015
Commented:
Try something like this...

Sub SelectTextBetweenBookmarks()
Dim doc As Document
Dim Rng As Range
Dim bm1 As Bookmark, bm2 As Bookmark

Set doc = ActiveDocument
Set bm1 = doc.Bookmarks("StartCopy")
Set bm2 = doc.Bookmarks("EndCopy")

Set Rng = doc.Range(bm1.End, bm2.Start)
Rng.Select

End Sub

Open in new window

GrahamSkanRetired
Top Expert 2012
Commented:
It isn't clear whether you want to include the contents of each bookmark.

This macro  assumes that you don't
Sub SelectdBookMarkGap()
    Dim rngA As Range
    Dim rngB As Range
    Dim rngC As Range
    
    Set rngA = ActiveDocument.Bookmarks("StartCopy").Range
    Set rngB = ActiveDocument.Bookmarks("EndCopy").Range
    
    Set rngC = ActiveDocument.Range
    
    rngC.Start = rngA.End
    rngC.End = rngB.Start
    rngC.Select
End Sub

Open in new window

GrahamSkanRetired
Top Expert 2012

Commented:
Sorry Subodh Tiwari (Neeraj).
I note that your code make the same assumption as mine did, but does it in a slightly different way
Acronis in Gartner 2019 MQ for datacenter backup

It is an honor to be featured in Gartner 2019 Magic Quadrant for Datacenter Backup and Recovery Solutions. Gartner’s MQ sets a high standard and earning a place on their grid is a great affirmation that Acronis is delivering on our mission to protect all data, apps, and systems.

Author

Commented:
Thank you. Both solutions are working fine. I wonder if I could request one addition to this code. After Selecting all the text between these two Bookmarks I would like to be able to Delete it, but without deleting the Bookmarks. At present if after Selecting Text, I say
Selection.Delete
it deletes the text, but it also deletes one or  other Bookmark.  Can we delete text, without deleting the relevant Bookmarks.

Author

Commented:
I have realised Graham's code is not selecting Bookmarks, which is what I need. I am just test it.

Author

Commented:
Having test Graham's code I notice that with it also if I say
rngC.Delete

It deletes the first Bookmark also, in addition to the selected text.
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
Then try this...

Sub SelectTextBetweenBookmarks()
Dim doc As Document
Dim Rng As Range
Dim bm1 As Bookmark, bm2 As Bookmark

Set doc = ActiveDocument
Set bm1 = doc.Bookmarks("StartCopy")
Set bm2 = doc.Bookmarks("EndCopy")

Set Rng = doc.Range(bm1.End, bm2.Start)
Rng.Delete

End Sub

Open in new window


I have realised Graham's code is not selecting Bookmarks, which is what I need. I am just test it.
Do you mean the code I suggested selected any of bookmarks?
That's not true. I tested the code and it doesn't select any of the bookmarks and deletes the text between those two bookmarks.

Author

Commented:
I don' t know what is going on here. Both solutions make sense and should not select the Bookmarks. However, I have tested both solutions many times the first Bookmark StartCopy does get deleted when I say Selection.Delete at the end of both Solutions.

Author

Commented:
I wonder if the difference is in the way I am insert Bookmarks.
The way I am doing is I go to the Start of the Document and and insert StartCopy Bookmark. I then move the cursor a few characters to the right and insert the EndCopy Bookmark.
GrahamSkanRetired
Top Expert 2012

Commented:
That is the normal way to add bookmarks manually.

Bookmarks are a part of the range, so they are liable to deletion. I have a small routine that I use to replace the bookmark after its contents have been deleted.
Sub DeleteBookMarktext(bmk As Bookmark)
    Dim strName As String
    Dim rng As Range
    
    strName = bmk.Name
    Set rng = bmk.Range
    rng.Delete
    rng.Document.Bookmarks.Add strName, rng
End Sub

Open in new window

It will be a little more complicated to consider two bookmarks, but do you really need to use two?  Select all the text that you want to bookmark and then insert the one bookmark. It will contain all the selected text.

You can call the macro above so:
DeleteBookMarktext Activedocument.Bookmarks("MyBookmark")

Open in new window

Author

Commented:
Many thanks. As both solutions and additional comments were very helpful, I am splitting the points. Hope this is OK. I am very grateful for help.
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
You're welcome Faheem!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial