Help with a next-word Word Macro?

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?
Cindy GiovanettiTranscriptionAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
1. Please post your current code.

2. It would be helpful if you provided some examples with different punctuation and spacing.
Cindy GiovanettiTranscriptionAuthor 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
Joe HowardCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

aikimarkCommented:
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 GiovanettiTranscriptionAuthor 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 GiovanettiTranscriptionAuthor 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 GiovanettiTranscriptionAuthor 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 GiovanettiTranscriptionAuthor 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?
Joe HowardCommented:
Just replace every occurrence of 1 in my code with -1.
Cindy GiovanettiTranscriptionAuthor Commented:
Oh!  I was never going to think of that!  Thank you!  I'll let you know tomorrow if it works!
Cindy GiovanettiTranscriptionAuthor 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
Joe HowardCommented:
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 GiovanettiTranscriptionAuthor 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
aikimarkCommented:
@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 GiovanettiTranscriptionAuthor 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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.