Here are a few "swapping" macros to help you get started in customizing your Word experience.
Word swap in Word 2016
Here is a handy macro that you'll probably use all the time. The word_swap macro swaps two words. It cuts the first word and then pastes it after the second word:Sub word_swap()
'
' word_swap Macro
' Swap two words, left-right
'
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Cut
Selection.MoveRight Unit:=wdWord, Count:=1
Selection.Paste
End Sub
These keystrokes were recorded to make this macro:
- Ctrl+Shift+→ The word to the right of the cursor is selected.
- Ctrl+X The word is cut.
- Ctrl+→ The cursor moves after the second word.
- Ctrl+V The original word is pasted.
Word macros cannot record mouse clicks. When you need to select text, use the cursor keys plus the Shift key, or use the F8 (extended selection) key.
Also, for this macro to work, the insertion pointer must be positioned at the start of the first word.And/or word swap in Word 2016
Another word swap macro that you'll probably use frequently the and_or_word_swap macro. Unlike a regular word swap, the goal with this macro is to swap words on either side of a conjunction. For example, changing this or that to that or this.As with the word_swap macro, this macro was recorded from keystroke input:
Sub and_or_word_swap()
'
' and_or_word_swap Macro
' Swap two words in a conjunction
'
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Cut
Selection.MoveRight Unit:=wdWord, Count:=1
Selection.Paste
Selection.MoveRight Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Cut
Selection.MoveLeft Unit:=wdWord, Count:=2
Selection.Paste
End Sub
Here are the keystrokes used to record this macro:
- Ctrl+Shift+→
- Ctrl+X The first word is cut.
- Ctrl+→ The insertion pointer hops over the conjunction, and or or.
- Ctrl+V The word is pasted after the conjunction.
- Ctrl+Shift+→
- Ctrl+X The word after the conjunction (now after the first word you pasted in Step 4) is selected and cut.
- Ctrl+←, Ctrl+← The cursor moves back to just before the conjunction.
- Ctrl+V The second word is pasted.
For this macro to be effective, the insertion pointer must blink at the start of the first word.
Swap sentences in Word 2016
Just as you can swap two words in a row, you can also swap two sentences. The swap_sentences macro does just that. And, as in other text manipulation macros, use the keyboard — not the mouse — to select text.In the following code, the Selection.Extend command represents pressing the F8 key on the keyboard. When you press that key three times, a sentence is selected.
Sub swap_sentences()
'
' swap_sentences Macro
' Swap two sentences
'
Selection.Extend
Selection.Extend
Selection.Extend
Selection.Cut
Selection.Extend
Selection.Extend
Selection.Extend
Selection.EscapeKey
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Paste
End Sub
Here are the keystrokes recorded to create the swap_sentences macro:
- F8, F8, F8 The current sentence is selected.
- Ctrl+X
- F8, F8, F8 The next sentence is selected.
- Esc, → The selection is canceled, and the insertion pointer is placed at the start of the next sentence.
- Ctrl+V The first sentence is pasted after the second sentence.
Swap header and footer text in Word 2016
The swap_header_footer macro swaps the document's header text and footer text. You could complete this process manually, but the problem is that the macro doesn't accurately record all the actions. So, although you can record the basic keystrokes, you must return to the Visual Basic Editor to complete the macro:Sub swap_header_footer()
'
' swap_header_footer Macro
' Exchange header/footer text
'
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If
If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.WholeStory
Selection.Cut
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
Selection.HomeKey Unit:=wdLine
Selection.Paste
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Cut
ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
Selection.Paste
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
End Sub
The overall effect of this macro is to edit the document's header, select and cut all that text, and then switch to the footer. Once in the footer, the header's text is pasted, and then the footer's text is selected and cut. The macro switches back to the header and pastes the footer's text. Then the macro closes the header.