Heading

This is some text inside of a div block.
This is some text inside of a div block.
This is some text inside of a div block.
min read

Navigate or select by punctuation

Word • Macros • Editing
Peter Ronhovde
9
min read

Improving document navigation will contribute to editing more efficiently. We create several macros to jump or select to specific punctuation marks reducing how often we need to click around with the mouse.

Thanks for your interest

This content is part of a paid plan.

Navigate or select by punctuation

Efficiently navigating a document will obviously help an author edit faster. The faster we get to the target; the earlier we can make changes. Yes, Word has a GoTo dialog, but can you say … slow … ly? In an ideal world, we could just look and click, but that technology doesn't exist yet. Voice commands are improving, but they're still a little clunky when they don't understand you.

We can, however, use some more targeted tricks to speed up our document navigation. Toward that goal, this set of macros focuses on jumping to specific punctuation, reducing how often we need to click around with the mouse (which is slower than it seems compared to tapping a keyboard shortcut).

Jump to next comma macro example
Jump to next comma macro example

The bulk of the article will present the topic in the context of navigating to a comma, but the final macros extend easily to any punctuation mark as well as selections. If you prefer, the improved member version can iterate through successive marks making it a little faster and more intuitive to use.

Create the empty macro

A previous article shows how to create empty Word macros including an associated keyboard shortcut. Our first candidate macro skeleton is:

Sub GotoNextComma()
' Jump to the next comma in the document

End Sub

As usual, the empty line is waiting for our amazing command.

Jump to next punctuation mark

We’ll have four slight variations for navigating or selecting to punctuation because we need each option implemented for both forward and backward in the document. Commas are probably the most common use case, so we’ll focus on them for concreteness; but I’ve also created variations for parentheses, periods, quotes, and a few other punctuation marks.

The basic command to move the insertion point (the blinking I-bar not the mouse cursor) to a specific character (or set of characters) is MoveUntil:

Selection.MoveUntil ' Not done

The MoveUntil command moves the insertion point in the document until it finds any of the characters in a given list skipping everything in between. Toward that end, it requires a set of characters as an option named Cset. As advertised, we're using a comma here ",", so Cset := ",". This double quote notation is a standard way to designate a plain text "string" of characters even though this one only contains a single character. The revised command is:

Selection.MoveUntil Cset:=","

The colon equals := symbol assigns Cset the given value. In this case, we literally assigned it a comma character "," in double quotes.

Jump backward

To move backward in the document, we keep the MoveUntil command but add a Count option:

Selection.MoveUntil Cset:=",", Count:=wdBackward

Count is the maximum number of characters the command is allowed to move (defaults to moving forward by any number of characters), but the wdBackward constant (from a Word constants table) tells the command to move backward in the document to the first character in Cset that is found. If the command cannot find any of the indicated characters before that number of characters are checked in the document, nothing happens. Use smaller numbers if you want to limit how far the command can move.

Multiple characters

We’re not limited to a single character. Add as many you wish. For example, you might want to make your version stop at the next comma, period, or semicolon (perhaps so you have fewer keyboard shortcuts to remember). With this in mind, just add the extra characters to the Cset list within the double quotes.

Selection.MoveUntil Cset:=",.;"

This version of the command moves the insertion point until any of the characters in the set are found. It is not looking for string of characters equal to Cset. For example, setting Cset equal to "and" would look for the letter "a" or "n" or "d", whichever comes first, not the word "and". The latter would use the VBA equivalent of Word's Find feature.

Some special characters require a miscellaneous constant. For example, a paragraph marker is vbCr, and we would have to add it to the character set using a plus + sign like Cset := "," + vbCr.

Gotchas

Don't forget to consider possible problems before signing off on finished macros.

What if the punctuation mark does not exist?

If the punctuation mark does not exist in the searched direction within the character count limit, the move command will simply not do anything, so we're perfectly okay.

What if an initial selection exists when moving?

Having an initial selection can cause minor problems for some macros. Fortunately, the MoveUntil command just collapses any initial selection and moves the insertion point to the designated character.

There is a teensy weensy detail about whether it collapses toward the beginning or ending of the initial selection, but we won't worry about that detail in these macros. It rarely makes a practical difference. If you really want to control it to that level of detail, collapse the selection yourself using the Collapse method:

Selection.Collapse ' Collapse selection toward the start
Selection.Collapse Direction:=wdCollapseEnd ' Collapse selection toward the end

We won't go into much detail about these commands here, but there isn't much to say anyhow. The former variation collapses the selection toward the beginning which is the default behavior. The latter uses the Direction option to manually collapse toward the end of the selection.

Select to punctuation marks

A common use case for a selection variation of these macros is selecting all text up to the comma of an introductory phrase.

Select by punctuation macro example
Select by punctuation macro example

Select forward in the document

If we want to select all text forward to a punctuation mark, the command is almost the same except we tell it to move the End marker of the Selection. The command is MoveEndUntil:

  Selection.MoveEndUntil Cset:=","

Since the Start position is not changing, moving the End position extends the Selection to span all document content in between. If an initial selection exists, it just extends the selection forward.

Select backward in the document

To select backward, move the Start position of the Selection instead using the MoveStartUntil method, but we need to tell it to count backwards like we did with the navigation earlier.

  Selection.MoveStartUntil Cset:=",", Count:=wdBackward

The command leaves the End position unchanged which extends the selection.

Simple version issues

These simpler versions won’t jump past the character once it’s found the first time. The improved member versions extend this idea by allowing the user to intuitively skip the target character if it is immediately at the respective Start or End position depending on the macro version. That is, if you tap the keyboard shortcut again, perhaps expecting it to move or select up to the next comma, the macro won’t change the insertion point or selection because it “finds” the character immediately.

My personal versions allow extension or contraction of the selection in both directions, but those implementations seem overly complicated for presentation purposes. If there is enough interest, they could be made available as member plus articles.

Final navigation and selection macros

The final versions of the macros are amazingly just one-liners. Unfortunately, since VBA does not allow macro parameters when they are assigned to keyboard shortcuts, we need one version for each direction, for each combination of punctuation marks, and for navigating to or selecting text. I’ve only included the variations for commas here.

Navigate to a punctuation mark

To jump to the next comma in the document, use:

Sub GotoNextComma()
' Jump to the next comma in the document
Selection.MoveUntil Cset:=","
End Sub

Moving backward in the document:

Sub GotoPreviousComma()
' Jump to the previous comma in the document
Selection.MoveUntil Cset:=",", Count:=wdBackward
End Sub

I assigned these to keyboard shortcuts Command+, and Option+, respectively, in Word for Mac. In Windows, I assigned them to Control+, and Alt+, respectively.

Select up to a punctuation mark

To select from the current position forward to the next comma in the document, use:

Sub SelectToNextComma()
' Select text forward to the next comma in the document
Selection.MoveEndUntil Cset:=","
End Sub

Select backward in the document:

Sub SelectToPreviousComma()
' Select text backward to the previous comma in the document
Selection.MoveStartUntil Cset:=",", Count:=wdBackward
End Sub

In Word for Windows, I assigned this pair of keyboard shortcuts to Control+Shift+, and Alt+Shift+, respectively. In Word for Mac, I've tried a few variations, but I have not been satisfied with the choices because MacOS has trouble interpreting many shortcut combinations that involve punctuation keys. Right now, I'm using Command+Shift+, and Option+Control+Shift+, respectively.

Improvements

Tailoring the macros to match our personal workflow is one of the advantages of creating our own. Here are a few ideas of how you might tweak macros to match your own preferences.

Select whole words

Word will automatically selects whole words when making a selection by default (the feature can be turned off). If you want to emulate this behavior in your own macros, you can automatically select the whole first word even if you start inside the word. In this case just add an Expand command at the beginning of your macro using a word unit size.

Selection.Expand Unit:=wdWord

We won't go into any depth about the command at this time because it is a side trail. Personally, I have mixed feelings on Word automatically selecting whole words for me, but I have implemented similar things a few times in my own macros.

Position tweaks

My personal implementation adds some position tweaks, so the cursor usually ends on the left side of a comma or close parenthesis regardless of the direction of the jump. It’s just a tweak, but the little niceties add up, and why not let the macro do it for me rather than having to hit another key after I run the macro?

In the GotoPreviousComma version, I add a MoveLeft command to the end of the macro:

Selection.MoveLeft

This command accidentally approximates one of the extra features of the member versions. It allows me to repeatedly run the macro to jump to successive commas when moving backward. The member version adds this feature to all of the variations.

Create the variations you'll use

With all four variants, you’ll have a nice little set of navigation and selection utility macros. Once you have the basic macros, it's easy to add more character variations, so make whatever copies you like for the other punctuation marks you prefer. Commas are most common, of course, but I've occasionally used semicolons and parentheses as well.

Unless you combine several into the fewer macros, you’ll need to have separate small macros for each punctuation mark and direction. This is a consequence of a VBA limitation where we are not allowed to use parameters for any macro assigned to a keyboard shortcut. There is also a technical, somewhat vague limit to the number of macros we can store, but it seems to be high. Just implement the ones you know you’ll use. Other variations are easy to add later if you find yourself wanting them.

Managing shortcuts for a large number of macros is easier using our mostly member series on creating easy shortcuts in VBA.

Related macros

A sibling macro navigates to empty or incomplete paragraphs, and others jumps to paragraphs with a specific style or between headings. Plus more.

Now go forth and edit faster!

Affiliate Links

If you're interested in using Word or another tool related to the article, check out these affiliate links. I may make a small commission if you purchase when using them, but there is no increase in cost for you, and it helps to support this site and associated content.

I've been using Microsoft for Business for commercial use (that's us writers) on one of the lower pricing tiers for years. I get to use my macros, have online storage, and don't have to worry about software updates.