Avatar of FaheemAhmadGul
FaheemAhmadGul
Flag for United Kingdom of Great Britain and Northern Ireland asked on

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

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.
Microsoft OfficeVBAMicrosoft WordMicrosoft Applications

Avatar of undefined
Last Comment
Subodh Tiwari (Neeraj)

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Subodh Tiwari (Neeraj)

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
GrahamSkan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
GrahamSkan

Sorry Subodh Tiwari (Neeraj).
I note that your code make the same assumption as mine did, but does it in a slightly different way
FaheemAhmadGul

ASKER
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.
FaheemAhmadGul

ASKER
I have realised Graham's code is not selecting Bookmarks, which is what I need. I am just test it.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
FaheemAhmadGul

ASKER
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)

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.
FaheemAhmadGul

ASKER
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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
FaheemAhmadGul

ASKER
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.
GrahamSkan

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

FaheemAhmadGul

ASKER
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.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Subodh Tiwari (Neeraj)

You're welcome Faheem!