Link to home
Start Free TrialLog in
Avatar of FaheemAhmadGul
FaheemAhmadGulFlag 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.
ASKER CERTIFIED SOLUTION
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry Subodh Tiwari (Neeraj).
I note that your code make the same assumption as mine did, but does it in a slightly different way
Avatar of 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.
I have realised Graham's code is not selecting Bookmarks, which is what I need. I am just test it.
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.
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.
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.
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.
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

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.
You're welcome Faheem!