Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MoveEndUntil: Move End until defined string (not just a char)

Status
Not open for further replies.

PhilTUV

Technical User
Apr 1, 2010
2
DE
In MS Word VBA, I require a function which moves the end of the selection until a defined string (not merely one out of a set of characters, as does MoveEndUntil).

Say, for example, I have following text:

/foo <lots of nonsense in between, such as /x> /bar

If I run

Code:
selection.moveenduntil CSet:="/bar", Direction:=wdForward

,the end stops at /x (at /, to be precise). I want to select all the text between /foo and /bar and process it further. I've seen a few good posts in this forum on this matter, but afaik no conclusion on how to master this. Any help appreciated.
 
CSet is a single character function. You can not do this with CSet. It can be done however.

Say you have:

/foo <lots of nonsense in between, such as /x> /bar and other stuff after.

and the Selection is at just before "/foo", then the following will make the Selection cover up to and including "/bar"

Code:
Sub FromHereToThere()
Dim r As Range

Set r = ActiveDocument.Range( _
   Start:=Selection.End, _
   End:=ActiveDocument.Range.End)
With r.Find
   .Text = "/bar"
   .Execute
   If .Found = True Then
      Selection.End = r.End
   End If
End With
End Sub
This code uses a range object from the end of the current Selection, to the end of the document. If you would like, you could limit that to a smaller range (the next three paragraphs for example). A little trickier to code, but do-able.

Gerry
 
Hey fumei,

that's excellent. Thank you for your help! That does exactly what I want to.

I was wondering if there was something else you knew something about - say I want to select all text until I find a bookmark (_any_ bookmark). Could I pick your brains on that?

Phil.
 
Sure. It is even easier. Assuming the bookmark is ahead of the Selection!...
Code:
Selection.End = ActiveDocument.Bookmarks("whatever") _
     .Range.Start
This also assumes you want the selection to end at the START of the bookmark.

Technically, good practice would have some error trapping to test if the bookmark is indeed after the Selection. OR, you could make the Selection select backwards, but YOU must decide the logic.
Code:
Sub SelectToBookmark()
Dim strBM_Name As String
strBM_Name = InputBox("Bookmark name please.")
If ActiveDocument.Bookmarks(strBM_Name).Range.Start _
   < Selection.Start Then
   Selection.Start = ActiveDocument.Bookmarks(strBM_Name).Range.Start
Else
   Selection.End = ActiveDocument.Bookmarks(strBM_Name) _
     .Range.Start
End If
End Sub
The above gets a bookmark name from an InputBox, and if the bookmark start is less than the Selection - i.e. it is before the Selection - then the Selection is expanded backwards to the bookmark Start.


This could just as easily be the bookmark end...whatever.

If the bookmark is after the Selection, the Selection is expanded forward to the Start of the bookmark.

This could just as easily be the bookmark end...whatever.

The point being is that bookmark Start, or End, is simply a number. That is it. As is the Selection Start or End. Just a number. Therefore if you want to change it...change it.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top