Help with a next-word Word Macro?

Cindy Giovanetti
Cindy Giovanetti used Ask the Experts™
on
Would anyone care to  help me with a Word macro that jumps to the end of the next word?

Ctrl-Right arrow will do this out of the box, BUT it goes to the BEGINNING of the next word.  I want to stop at the END of the next word.

So I wrote a fairly simple macro that uses Ctrl-Right to go to the beginning of the next word, and then a left arrow to go back one space.  And that works great unless there is punctuation involved or two spaces at the end of a sentence (which I use).  In that case, my macro leaves me in the middle of a word or in between the two spaces.

I know there is programming code for find the end of a word and find the beginning of a word or find the next character that's not a space.  But a problem that I'm having is that Ctrl-Right stops before a punctuation mark.  I need my macro to stop after the punctuation mark.  Ctrl-Right won't do that.  

Can anyone help?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2014

Commented:
1. Please post your current code.

2. It would be helpful if you provided some examples with different punctuation and spacing.
Cindy GiovanettiTranscription

Author

Commented:
Sure.  Code below.  But I think I need something totally different.  Thanks for being willing to help.  :)

Sub nextword()

'
' nextword Macro
'

    Selection.MoveRight Unit:=wdCharacter, Count:=1
    Selection.MoveRight Unit:=wdWord, Count:=1
    Selection.MoveLeft Unit:=wdCharacter, Count:=1
 
End Sub
Try this
Sub MoveToEndOfWord()
    If Selection.Text = " " Then
        Selection.MoveRight Unit:=wdWord, Count:=1
    End If

    Selection.MoveRight Unit:=wdWord, Count:=1
    Selection.MoveLeft Unit:=wdCharacter, Count:=1

    If Selection.Text <> " " Then
        Selection.MoveRight Unit:=wdCharacter, Count:=1
    End If
    Select Case Selection.Text
        Case Is = ".", ","
            Selection.MoveRight Unit:=wdCharacter, Count:=1
    End Select
End Sub

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Top Expert 2014

Commented:
This works in my test document.
Selection.MoveRight wdWord, 1
Selection.MoveRight wdCharacter, len(selection.Words(1)) - iif(Right(Selection.Words(1),1) = " ", 1, 0)

Open in new window

Cindy GiovanettiTranscription

Author

Commented:
MacroShadow!!  It worked!  Thrilled!

Aikimark, I didn't even try your suggestion because the first suggestion worked, but I might be back for more advice.  Thank you both so much!
Cindy GiovanettiTranscription

Author

Commented:
I'm very happy to have this!  I've been frustrated with the need for that for a long time.

And, Joe, thank you for introducing me to the hyper key.  I had never used it, and it's going to be my new favorite.

I have another Word macro request, but I'll start a new thread.
Cindy GiovanettiTranscription

Author

Commented:
MicroShadow, could I trouble you to give me another version of that same macro that goes to the left in the same way, stopping at the end of each word?  

I've tried to do it myself, and I can't make it work.  (I'm not a programmer.)
Cindy GiovanettiTranscription

Author

Commented:
So here's what I've come up with to move the cursor to the left, stopping at the end of the previous word or to the right of a punctuation mark if there is one.

Ctrl-left arrow (moves the cursor to the beginning of the previous word),
If the character to the left of the cursor is a space, go left one space.
If the character right of the cursor is a punctuation mark, go to the right one character.

I don't know how to put this into syntax.  I think all the logic is there.  ??  Could somebody help?
Just replace every occurrence of 1 in my code with -1.
Cindy GiovanettiTranscription

Author

Commented:
Oh!  I was never going to think of that!  Thank you!  I'll let you know tomorrow if it works!
Cindy GiovanettiTranscription

Author

Commented:
It did not work.  It does stop to the right of punctuation marks, which I want.  But it stops at the front of words rather than at the end.  Here's my code.  Would somebody help?

Sub MoveToEndOfprevWord()
    If Selection.Text = " " Then
        Selection.MoveRight Unit:=wdWord, Count:=-1
    End If

    Selection.MoveRight Unit:=wdWord, Count:=-1
    Selection.MoveLeft Unit:=wdCharacter, Count:=-1

    If Selection.Text <> " " Then
        Selection.MoveRight Unit:=wdCharacter, Count:=-1
    End If
    Select Case Selection.Text
        Case Is = ".", ",", "!", "?", """"
            Selection.MoveRight Unit:=wdCharacter, Count:=-1
    End Select
    End Sub
Does simply this work? If not please give sample text to work with.
    Selection.MoveLeft Unit:=wdWord, Count:=1
    Selection.MoveLeft Unit:=wdCharacter, Count:=1

Open in new window

Cindy GiovanettiTranscription

Author

Commented:
Hi Joe.  That works about 80% of the time.  It is thrown off by punctuation and the spacing after punctuation.

Here's an example showing everywhere that macro stopped.  I started with the selection point at the END of the document where the blue arrow is and used your code to walk backwards.  It stopped everywhere I inserted a caret.

Capture.PNG
Top Expert 2014

Commented:
@Cindy

1. Since this is a 'solved' question, should we open it up so that you get the solution you need?

2. I'm a bit confused by your example (which would be much easier to use if it were words, rather than an image of words).  Do you actually have a document with carats?

3. The position of the selection in the second word, "new", is most likely due to the lack of a check to see if the last character in the selected word ends with a space.  You can't just back up 1 character every time, only when the last character is a space.

4. Since you never posted an example of what the correct/desired behavior is supposed to be, we are left to play a guessing game about the accuracy of our posted solutions.
Cindy GiovanettiTranscription

Author

Commented:
I'm sorry.  I'm new to the forum.  Let me start a new post.  Let's leave this one closed because I did get a good answer here.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial