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.
LVL 1
FaheemAhmadGulAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GrahamSkanRetiredCommented:
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

0
GrahamSkanRetiredCommented:
Sorry Subodh Tiwari (Neeraj).
I note that your code make the same assumption as mine did, but does it in a slightly different way
0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

FaheemAhmadGulAuthor 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.
0
FaheemAhmadGulAuthor Commented:
I have realised Graham's code is not selecting Bookmarks, which is what I need. I am just test it.
0
FaheemAhmadGulAuthor 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.
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
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.
0
FaheemAhmadGulAuthor 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.
0
FaheemAhmadGulAuthor 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.
0
GrahamSkanRetiredCommented:
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

0
FaheemAhmadGulAuthor 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.
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
You're welcome Faheem!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.