How to Write Word 2016 Macros to Swap Text Elements

By Dan Gookin

When working with documents in Word 2016, especially longer ones, you’ll want the extra ease and FUNctionality of macros that can swap text words, phrases, or elements around. To create macros, you’ll probably need the Visual Basic for Applications (VBA) programming language, which is vast, complex, and intimidating. That means it has great potential, but isn’t something that you’ll sit and learn in a casual afternoon.

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:

  1. Ctrl+Shift+
    The word to the right of the cursor is selected.
  2. Ctrl+X
    The word is cut.
  3. Ctrl+
    The cursor moves after the second word.
  4. 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:

  1. Ctrl+Shift+
  2. Ctrl+X
    The first word is cut.
  3. Ctrl+
    The insertion pointer hops over the conjunction, and or or.
  4. Ctrl+V
    The word is pasted after the conjunction.
  5. Ctrl+Shift+
  6. Ctrl+X
    The word after the conjunction (now after the first word you pasted in Step 4) is selected and cut.
  7. Ctrl+, Ctrl+
    The cursor moves back to just before the conjunction.
  8. Ctrl+V
    The second word is pasted.

The net effect of these keyboard shortcuts is to cut a word on one side of an and or or and then paste the word on the other side. Then the second word is cut and pasted before the and or or.

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:

  1. F8, F8, F8
    The current sentence is selected.
  2. Ctrl+X
  3. F8, F8, F8
    The next sentence is selected.
  4. Esc,
    The selection is canceled, and the insertion pointer is placed at the start of the next sentence.
  5. Ctrl+V
    The first sentence is pasted after the second sentence.

When you run this macro, ensure that the insertion pointer is set somewhere within the first 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.