Cindy Giovanetti
asked on
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?
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?
ASKER
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
Sub nextword()
'
' nextword Macro
'
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveRight Unit:=wdWord, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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)
ASKER
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!
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!
ASKER
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.
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.
ASKER
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.)
I've tried to do it myself, and I can't make it work. (I'm not a programmer.)
ASKER
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?
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.
ASKER
Oh! I was never going to think of that! Thank you! I'll let you know tomorrow if it works!
ASKER
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
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
ASKER
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.
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.
@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.
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.
ASKER
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.
2. It would be helpful if you provided some examples with different punctuation and spacing.