MS Office Forum / Word / Programming / November 2004
Select next word in a cell
|
|
Thread rating:  |
Dave Neve - 20 Nov 2004 21:41 GMT Hi
How do you select the next word in a cell?
I want the cursor to move right until it comes to the next letter and then select that word.
Thanks in advance
Dave
Helmut Weber - 21 Nov 2004 00:52 GMT Hi Dave, Selection.MoveRight Unit:=wdWord, Count:=1 Selection.Words(1).Select But to prevent the selection from selecting all of the cell, which would happen, if it is on the last word, not to mention what happens if it were in an empty cell, would be quite another story. --- Greetings from Bavaria, Germany Helmut Weber, MVP "red.sys" & chr(64) & "t-online.de" Word XP, Win 98 http://word.mvps.org/
Doug Robbins - 21 Nov 2004 04:26 GMT The following code will select the word in the cell that follows the word in which the selection is currently located:
Dim i As Long, j As Long, pararange As Range For i = 1 To Selection.Cells(1).Range.Words.Count Set myrange = Selection.Cells(1).Range.Words(i) If i < Selection.Cells(1).Range.Words.Count - 1 And InStr(myrange, Selection) > 0 Then Selection.Cells(1).Range.Words(i + 1).Select Exit For End If Next i
 Signature Please respond to the Newsgroup for the benefit of others who may be interested. Questions sent directly to me will only be answered on a paid consulting basis.
Hope this helps, Doug Robbins - Word MVP
> Hi > [quoted text clipped - 6 lines] > > Dave Dave Neve - 21 Nov 2004 08:36 GMT Hi
Thanks to both of you for coming to my aid again. Helmet was right in that his macro presented me with certain problems for what I want to do.
Dougs macro seems to work fine except that when applied to the last word in a cell, it seems to select the word before.
I am really sorry but I can't figure out how to stop this in view of
Selection.Cells(1).Range.Words(i + 1).Select.
Why does i + 1 become i - 1 if it is the last word.
Thanks in advance
> The following code will select the word in the cell that follows the word > in which the selection is currently located: [quoted text clipped - 19 lines] >> >> Dave Helmut Weber - 21 Nov 2004 12:02 GMT Hi Dave, just for fun, and I wonder what your test results will show, a much less smart approach than Doug's, kind of brute force: Sub test() ' wdEndOfRangeColumnNumber = 17 ' wdEndOfRangeRowNumber = 14 Dim r As Range Dim iCll As Integer ' cell Dim iRow As Integer ' row ResetSearch With Selection If .Range.Text = .Cells(1).Range.Text Then MsgBox "all of cell selected" Exit Sub End If Set r = .Range .Collapse direction:=wdCollapseEnd iCll = .Information(17) iRow = .Information(14) With .Find .Text = "<*>" .MatchWildcards = True .Execute End With If iCll <> .Information(17) Or iRow <> .Information(14) Then r.Select End If End With ResetSearch End Sub '--- Public Sub ResetSearch() With Selection.Find .ClearFormatting .Replacement.ClearFormatting .Text = "" .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute End With End Sub Have a nice day, both of you! --- Greetings from Bavaria, Germany Helmut Weber, MVP "red.sys" & chr(64) & "t-online.de" Word XP, Win 98 http://word.mvps.org/
Dave Neve - 21 Nov 2004 18:23 GMT You have a strange idea of 'fun' Helmet but thanks anyway.
You too have a nice week.
Dave
> Hi Dave, > just for fun, and I wonder what your test results will show, [quoted text clipped - 51 lines] > Word XP, Win 98 > http://word.mvps.org/ Helmut Weber - 22 Nov 2004 12:32 GMT By the ways, my code seems to work alright. Did you try it? Helmut Weber
Doug Robbins - 21 Nov 2004 14:31 GMT The following modified macro overcomes that problem:
Dim i As Long, myrange As Range Selection.Words(1).Select For i = 1 To Selection.Cells(1).Range.Words.Count Set myrange = Selection.Cells(1).Range.Words(i) If i < Selection.Cells(1).Range.Words.Count - 1 And InStr(myrange, Selection) > 0 Then Selection.Cells(1).Range.Words(i + 1).Select Exit For End If Next i
 Signature Please respond to the Newsgroup for the benefit of others who may be interested. Questions sent directly to me will only be answered on a paid consulting basis.
Hope this helps, Doug Robbins - Word MVP
> Hi > [quoted text clipped - 36 lines] >>> >>> Dave Dave Neve - 21 Nov 2004 18:39 GMT Hi Doug
Unless I am going barmy, this macro still displays the same problem.
Any chance of another look at it?
Thanks
Dave
> The following modified macro overcomes that problem: > [quoted text clipped - 49 lines] >>>> >>>> Dave Doug Robbins - 21 Nov 2004 19:18 GMT Hi Dave,
I just tried it again and it seems to work OK whether there is nothing, a space or a carriage return after the last word in the cell.
What do you actually have in the cell?
 Signature Please respond to the Newsgroup for the benefit of others who may be interested. Questions sent directly to me will only be answered on a paid consulting basis.
Hope this helps, Doug Robbins - Word MVP
> Hi Doug > [quoted text clipped - 58 lines] >>>>> >>>>> Dave Dave Neve - 22 Nov 2004 08:22 GMT Hi
You're gonna think I am mad but the words in the cell make a difference to the behaviour of the macro.
If I type in the same word five times and execute the macro, it blocks on the second word!!!
If the words are different, it seems to execute except on some cells where I still get strange behaviour but can't work out why.
I suppose that as Word has a spell check which doesn't like repeated words, sth in Word stops the macro from running on certain occasions.
But on the whole the macro works thanks.
Dave
> Hi Dave, > [quoted text clipped - 65 lines] >>>>>> >>>>>> Dave Doug Robbins - 22 Nov 2004 10:32 GMT OK, I've just reproduced that behavior (with the same word repeated). It's bizarre.
 Signature Please respond to the Newsgroup for the benefit of others who may be interested. Questions sent directly to me will only be answered on a paid consulting basis.
Hope this helps, Doug Robbins - Word MVP
> Hi > [quoted text clipped - 83 lines] >>>>>>> >>>>>>> Dave Helmut Weber - 22 Nov 2004 19:54 GMT Hi Dave and Doug, the macro in discussion cannot get over a repeated word. Lets say the third word was selected, and the same word is before it, then, if the word(i) is in the selection, the word after word(i) would be selected, which could be the very selection itself, or, even over a distance, the word after word(i) again. So the selection would be back, were it was already once before. Nice little riddle. ;-) ---
>OK, I've just reproduced that behavior (with the same word repeated). It's >bizarre. Doug Robbins - 23 Nov 2004 01:10 GMT Hi Dave,
Fellow MVP, Jean-Guy Marcil, came up with the following which does the trick.
Sub SelectNextWordinCell()
Dim CellEnd As Long Dim WordEnd As Long Dim WordRge As Range
With Selection CellEnd = .Cells(1).Range.End WordEnd = .Words(1).End
If WordEnd < CellEnd - 1 Then Set WordRge = .Words(1) Set WordRge = WordRge.Next(wdWord, 1) WordRge.Select Else MsgBox "This is the last word in the cell." End If End With
End Sub
 Signature Please respond to the Newsgroup for the benefit of others who may be interested. Questions sent directly to me will only be answered on a paid consulting basis.
Hope this helps, Doug Robbins - Word MVP
> Hi > [quoted text clipped - 83 lines] >>>>>>> >>>>>>> Dave
|
|
|