Word macro to select segment smaller than a whole sentence

W

wtbx

Control+clicking on any part of a sentence selects the whole sentence,
using a full mark as a delimiter.

Is it possible to create a macro that would use a semicolon as segment
delimiter?

Thanks for any ideas

Wtbx
 
J

Jonathan West

wtbx said:
Control+clicking on any part of a sentence selects the whole sentence,
using a full mark as a delimiter.

Is it possible to create a macro that would use a semicolon as segment
delimiter?

Thanks for any ideas

Wtbx

Try this

Sub ExtendtoSemiColon
With Selection
.MoveEndUntil Cset:=";", Count:=wdForward
.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
End With
End Sub
 
F

Fumei2 via OfficeKB.com

Ctrl-click moves both forward and backward. In other words, if you are three
words into a sentence, Ctrl-Click will select the previous words, AS WELL AS
the remaining words of the sentence.

Jonathan's code moves forward to the first ";"

Is this sufficient? If not, please detail exactly what you are trying for.
To get the previous words of the sentence included, try:

With Selection
.Expand Unit:=wdSentence
.Collapse 1
.MoveEndUntil Cset:=";", Count:=wdForward
.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
End With

This expands to the whole sentence, collapses to the Start, then moves the
End to the first ";" (and then moves again to include the ";").

Jonathan said:
Control+clicking on any part of a sentence selects the whole sentence,
using a full mark as a delimiter.
[quoted text clipped - 5 lines]

Try this

Sub ExtendtoSemiColon
With Selection
.MoveEndUntil Cset:=";", Count:=wdForward
.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
End With
End Sub
 
D

Doug Robbins - Word MVP

The following code will select the text from the present location of the
insertion point (selection) to and including the next semicolon (if there is
one)

Dim myrange As Range
Set myrange = Selection.Range
myrange.End = ActiveDocument.Range.End
If InStr(myrange, ":") > 0 Then
myrange.End = myrange.Start + InStr(myrange, ";")
myrange.Select
Else
MsgBox "There is no semicolon between the location of the insertion
point and the end of the document."
End If


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
W

wtbx

Thank you both very much for you prompt replies and your help.

Fumei, your macro does almost what I'm looking for. Here is a better
explanation of what I'd like the macro to do:

Let's say we have the followingsentence, which includes 3 segments.

--- sentence starts here
This is the first segment; this is the second segment; this is the
third segment.
--- sentence stops here

I would like a macro that, when I click on any part of a particular
segment, would choose the whole of that segment.

So, if i clicked on the word "first", the macro would select: "This is
the first segment;"
If I clicked on the word "second", the macro would select: "this is
the second segment;"
If I clicked on the word "third", the macro would select: "this is the
third segment."

Do you think this could be done?

I appreciate any help, because I am totally VBA-illiterate but such a
macro would help me immensely in my work!

Thanks again for your time

Wtbx

Ctrl-click moves both forward and backward.  In other words, if you are three
words into a sentence, Ctrl-Click will select the previous words, AS WELLAS
the remaining words of the sentence.

Jonathan's code moves forward to the first ";"

Is this sufficient?  If not, please detail exactly what you are trying for.
To get the previous words of the sentence included, try:

With Selection
   .Expand Unit:=wdSentence
   .Collapse 1
   .MoveEndUntil Cset:=";", Count:=wdForward
   .MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
End With

This expands to the whole sentence, collapses to the Start, then moves the
End to the first ";" (and then moves again to include the ";").





Jonathan said:
Control+clicking on any part of a sentence selects the whole sentence,
using a full mark as a delimiter.
[quoted text clipped - 5 lines]
Try this
Sub ExtendtoSemiColon
 With Selection
   .MoveEndUntil Cset:=";", Count:=wdForward
   .MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
 End With
End Sub
 
W

wtbx

Thanks Doug!

I read your message after posting the above message.

Your macro does what I want except for selecting the whole of the
segment.

Could this be included in the macro, as I explained above?

Cheers

Wtbx
 
D

Doug Robbins - Word MVP

Creating a macro to select the whole of the second segment is quite straight
forward - the text between two semi-colons.

How though are you going to define with certainty what constitutes the start
of the first segment or the end of the last segment.

What would happen if your document contained multiple

This is the first segment; this is the second segment; this is the
third segment.
This is the first segment; this is the second segment; this is the
third segment.

and you try and select the whole of the third segment, you could end up
selecting:

this is the
third segment.
This is the first segment;

so you start having to introduce what may become quite complicated
conditions

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
W

wtbx

I see, Doug. Thanks for enlightening me. So the problem would be the
third segment, not the first or the second one.

If I am not asking too much, could you possibly write a macro for
selecting just the second segment, between two semi colons, given that
my texts have that kind of segments most of the time? (OK, a macro
able to select the first one or the second one would be great, but
that *would* definetely be asking too much, I guess)

Thank you for your time and effort!

Wtbx
 
G

Graham Mayor

How about a different approach?
In a given sentence containing at least one semi colon, the following should
select the segment the cursor is in (including the start and end of the
sentence).

Dim vSegment As Variant
Dim oRng As Range
Dim i As Long
vSegment = Split(Selection.Sentences(1), ";")
For i = 0 To UBound(vSegment)
Set oRng = Selection.Paragraphs(1).Range
With oRng.Find
.Text = vSegment(i)
Do While .Execute(Forward:=True) = True
If Selection.InRange(oRng) = True Then
oRng.Select
End If
Loop
End With
Next i

http://www.gmayor.com/installing_macro.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>



I see, Doug. Thanks for enlightening me. So the problem would be the
third segment, not the first or the second one.

If I am not asking too much, could you possibly write a macro for
selecting just the second segment, between two semi colons, given that
my texts have that kind of segments most of the time? (OK, a macro
able to select the first one or the second one would be great, but
that *would* definetely be asking too much, I guess)

Thank you for your time and effort!

Wtbx
 
W

wtbx

Thank you so much! It works like a charm! You have no idea how much
easier you've made my life :)

Wtbx
 
G

Graham Mayor

You are welcome :)

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>


Thank you so much! It works like a charm! You have no idea how much
easier you've made my life :)

Wtbx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top