Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-19-2017, 01:30 AM
zoowalk zoowalk is offline shortcut: Moving words within a text Windows 7 64bit shortcut: Moving words within a text Office 2013
Novice
shortcut: Moving words within a text
 
Join Date: Apr 2017
Posts: 3
zoowalk is on a distinguished road
Post shortcut: Moving words within a text

I am trying to create two keyboard shortcuts which allow me to move selected words quickly to the right and left within a text. The selected text should move one word to the left or the right. Here is what I want to do


1) Select words e.g. “this is” in the sentence “this is a tree”

2) Press e.g. ctrl + alt + arrow to the right

3) The sentence reads now as “a this is tree”



4) Press again ctrl alt + arrow to the right

5) The sentence reads now as “a tree this is”


The idea is to replace the cut / paste steps and make the process a bit more efficient and smoother. I have no knowledge in VB, but managed to get close to by using Word’s macro-function.


Sub moveRight() 'moveRight Macro Selection.Cut Selection.moveRight Unit:=wdWord, Count:=1 Selection.PasteAndFormat (wdFormatOriginalFormatting) End Sub

The problem with this function is that the selected words are no longer selected once they are pasted. Hence, triggering the function again (=moving the text more than one word) results in an error (I would have to select the relevant text again). Is there any way that the selected words remain selected after they are pasted so that I can trigger the function repeatedly?
Many thanks.
Reply With Quote
  #2  
Old 04-19-2017, 08:26 AM
slaycock slaycock is offline shortcut: Moving words within a text Windows 7 64bit shortcut: Moving words within a text Office 2013
Expert
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

<deleted - my bad>

Last edited by slaycock; 04-19-2017 at 08:31 AM. Reason: Not reading carefully enough and answering the wrong question
Reply With Quote
  #3  
Old 04-19-2017, 08:39 AM
zoowalk zoowalk is offline shortcut: Moving words within a text Windows 7 64bit shortcut: Moving words within a text Office 2013
Novice
shortcut: Moving words within a text
 
Join Date: Apr 2017
Posts: 3
zoowalk is on a distinguished road
Default

thanks for your reply. Either I didn't understand your reply, or my question was not clear enough.

The count property defines that the selected text 'moves' by one word to the right, once I make use of the keyboard shortcut. This is fine.

The problem is that once I triggered it, the previously selected text is no longer selected and hence can't be 'moved' when I trigger the command for a second time. I have to re-select the words.

I read the help file and I can't see how the count property relates to the selection of the words which I want to move. Am I missing something obvious?
Reply With Quote
  #4  
Old 04-19-2017, 08:45 AM
slaycock slaycock is offline shortcut: Moving words within a text Windows 7 64bit shortcut: Moving words within a text Office 2013
Expert
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

You need to make several changes

1. Remember the number of words in your original selection
2. after pasting the text move the start of the current selection backward the number of words you remembered.

Code:
Sub moveRight()
   'moveRight Macro
   
   Dim myWordsCount As Long
   
   myWordsCount = Selection.Range.Words.count
   Selection.Cut
   Selection.moveRight unit:=wdWord, count:=1
   Selection.PasteAndFormat (wdFormatOriginalFormatting)
   Selection.MoveStart unit:=wdWord, count:=-myWordsCount
   Selection.Select
End Sub
In future posts its helpful to use the Code tags (#)

Note the use of '-' in front of myWordsCount.
Reply With Quote
  #5  
Old 04-19-2017, 09:00 AM
zoowalk zoowalk is offline shortcut: Moving words within a text Windows 7 64bit shortcut: Moving words within a text Office 2013
Novice
shortcut: Moving words within a text
 
Join Date: Apr 2017
Posts: 3
zoowalk is on a distinguished road
Default

Excellent. Many thanks. Very helpful and now clear.
Reply With Quote
  #6  
Old 04-19-2017, 10:44 AM
slaycock slaycock is offline shortcut: Moving words within a text Windows 7 64bit shortcut: Moving words within a text Office 2013
Expert
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

Great. Apologies for the first post. I obviously didn't get to remove until after you'd read it.

The F1 for help on the term where the cursor is located is very useful. It does pay to read the help pages very carefully though.
Reply With Quote
Reply

Tags
paste problem, vba code



Similar Threads
Thread Thread Starter Forum Replies Last Post
shortcut: Moving words within a text Cannot see highlighted text for moving text in a do ument Jonfrank1@me.com Project 1 01-09-2017 06:04 PM
Selecting and moving text boxes identified by specific text. Chayes Word VBA 8 02-22-2016 07:01 AM
how to add text to a document without existing text moving around? Athalwolf Word 7 12-16-2014 05:16 PM
Shortcut problem > right to left text direction Sumodaddy Word 3 07-09-2014 06:51 AM
Shortcut to widen text boxes zarkomorin PowerPoint 0 08-22-2013 06:23 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:51 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft