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

Goto Heading

Word • Macros • Editing
Peter Ronhovde
6
min read

Sometimes it’s convenient to jump between headings in a document for faster navigation, so you don’t always have to grab the mouse to scroll or click somewhere.

This pair of one-line macros today very useful when you need them.

Thanks for your interest

This content is part of a paid plan.

Create the empty macro

A previous post covers creating an empty macro like the one shown below. When you’re done, you’ll have something like:

Sub GotoNextHeading()
  ' Jump to the next standard heading in the document
  ' with any heading level


End Sub

The single quote on the second line tells VBA to ignore the rest of the text on that line. Our VBA command goes on the empty line.

We’ll also need a similar one for jumping to the previous heading since you’ll probably want both.

Goto next heading standard command

Microsoft Word has seemingly left headings out of a lot of the regular Word keyboard shortcuts and commands. Fortunately, there is an obvious Word VBA command we can leverage to get what we want:

  Selection.Goto

This command works with a whole list of document content types, so we need to tell Word What content to jump to in the document.

  Selection.Goto What:=wdGoToHeading

There are also some other parameters to control the jump, but I’ll focus on what we need here for brevity.

A quick caveat is the Goto command when used for headings restricts us to jumping only to standard Word heading styles Heading 1 – 9. See the extended content below on how to accomplish this for any derived heading styles.

Goto previous heading

If we want the previous heading instead, we need to tell the command Which one to jump to.

There is yet another list of direction options, but we just need the constant for the previous occurrence of What content type we specified.

  Selection.Goto What:=wdGoToHeading, Which:=wdGoToPreviousHeading

Breaking up long command lines

The command is a little too long literally to fit on one line in some formats, but it will all be on one-line command in the VBA editor unless you actually hit return.

If you ever want to break up a single line in the VBA editor, use an underscore _ at the end of the continued line, and VBA will treat both as a single command line.

  Selection.Goto What:=wdGoToHeading, _
  Which:=wdGoToPreviousHeading

Final Goto Standard Headings Macros

The final macros to jump to the next standard heading is:

Sub GotoNextHeading()
  ' Jump to the next standard heading in the document
  ' with any heading level

  Selection.Goto What:=wdGoToHeading
End Sub

For the previous heading the macro is:

Sub GotoNextHeading()
  ' Jump to the next standard heading in the document
  ' with any heading level

  Selection.Goto What:=wdGoToHeading
End Sub

I assign these to Command+Option+Up and Command+Option+Down, respectively, in Word for Mac and Control+Alt+Up and Control+Alt+Down in Word for Windows.

I’m not advocating abandoning the mouse, but now in many cases, we can jump between headings and navigate our document faster using the keyboard.

Goto heading for derived styles

Fair warning here.

There are several alternate considerations mentioned below which make the comments stretch out a little bit, but I felt they needed to be said to be more complete with the explanations.

Sometimes the tweaks to get exactly what we want end up creating more complicated macros, but the final macros below are actually simple.

I just have to do a lot of talking to get there.

Goto command restriction

Selection.Goto gives detailed control over the content type and final locations for our navigation, but when used for headings, it only works for the standard Word heading styles Heading 1 – 9.

I don't like that.

Much of the time, I do use the standard heading styles, but I also use a lot of derived heading styles in my novels like Act, Chapter, Scene, Subscene, and probably others.

My version should ideally apply to any heading style even derived ones.

Fortunately, we have another tool at our disposal in the form of last week’s Select Headings article.

Bookmarks.

Bookmarks? Huh?

The previous article addresses why we’re using a Bookmark in a little more detail. I’ll give an abbreviated version here.

Word has some hidden predefined bookmarks which we can access in VBA. Specifically, we want to access the \HeadingLevel bookmark because bookmarks are essentially document Ranges with some extra stuff.

This command selects the current heading range under any heading style even derived ones.

  Selection.Bookmarks("\HeadingLevel").Select

Collapsing the selection

After using this bookmark command, the heading content range is now selected, but this macro is supposed to jump to the previous or next heading, not select it.

To finish our task, we can collapse the selection toward its start or end. The beginning is the default, so we just add this extra step:

  Selection.Collapse

Collapsing the selection is like hitting an arrow key when there is a selection. It just goes away.

In practice, collapsing this way would effectively position the insertion point at the beginning of the current heading content.

Collapsing to the end of the selection

If we need to jump to the end of the current heading content (the “next” heading), we instead collapse toward the end of the selection using the Direction parameter.

  Selection.Collapse Direction:=wdCollapseEnd

There are only two directions to collapse, so you don’t have to worry about any other options.

Isn’t this a hack solution?

I admit it may be a little confusing to first select something only to then collapse the Selection, but as far as I know, this is essentially the only way to jump to derived heading styles. Let me know if you’re aware of another method.

But even if we used the more intuitive Goto command using the hidden \HeadingLevel bookmark like:

  Selection.GoTo What:=wdGoToBookmark, Name:="\HeadingLevel"

This command still selects the whole bookmark Range on the jump because the bookmark spans a Range of text meaning we would still need to collapse the selection anyhow.

Just selecting the Range at the start of the macro gives the same result.

For macros this simple, you would hardly ever notice the selection and subsequent collapse happening, yet we get the additional functionality of using it for any heading style. A later article will cover how to avoid any potential issues with the user noticing the macro changes.

Goto previous heading gets stuck

One gotcha here is the jump to the previous heading will get “stuck” at the current heading because every time it is run, it will select the same heading content.

Then when you collapse the selection, you’re still at the same location.

A simple solution consists of moving one character to the left before running the other macro steps in the GotoPreviousHeading macro.

  Selection.MoveLeft  ' Avoid beginning of heading gotcha

This ensures that we will always move past the current heading if we are at its exact beginning, and it never hurts other cases if our insertion point is anywhere else inside the heading content.

Now I admit this is a hack fix in the spirit of duct tape handymen around the world (well, those that do VBA programming in Word).

There are definitely more elegant ways to solve the problem using Ranges, but this works for basically all cases when we haven’t officially introduced Ranges yet.

Final General Macros

The final macro to jump to the next heading of any type is:

Sub GotoNextHeading()
  ' Jump to the next heading in the document
  ' with the same heading level

  Selection.Bookmarks("\HeadingLevel").Select
  Selection.Collapse Direction:=wdCollapseEnd
End Sub

That’s the revised version. Have fun and let me know if you have any other special case ideas.

Sub GotoPreviousHeading()
  ' Jump to the previous heading in the document
  ' with any heading level

  Selection.MoveLeft  ' Avoid beginning of heading gotcha
  Selection.Bookmarks("\HeadingLevel").Select
  Selection.Collapse
End Sub

I assign these macros to Command+Option+Up and Command+Option+Down, respectively, in Word for Mac and Control+Alt+Up and Control+Alt+Down in Word for Windows.

Your insertion point can start at any location within the heading level, and it still jumps to the start of the indicated heading.

I use these more when editing blog or newsletter content but also in novel notes. I don’t need them all the time, but they’re definitely handy when I do.

Other caveats on how they work

Just to be clear, selecting the heading using the \HeadingLevel bookmark selects all subordinate content, even subheadings under your current heading level.

This creates another difference between this new GotoNextHeading macro and the original version using the Selection.GoTo command.

The new version will jump to the next heading with the same heading level as the current. That is, it will skip any heading levels with a lower outline level. On the other hand, the GoTo command will jump to the next heading of any level. This feature is, of course, a matter of preference.

There is also an asymmetry in how these new Next and Previous versions of the macros act, which bothers me, but the solution is beyond the scope of this article.

Upcoming members’ articles will cover more in depth extensions I’ve implemented which handle all the special cases consistently.

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.