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

Select Heading Content

Word • Macros • Editing
Peter Ronhovde
6
min read

What if I want to select a heading and its contents? Do I always have to grab the mouse (slow …) to select everything?

Of course not. That’s why you’re here, and this is a nice one-liner today.

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 SelectHeadingContent()
  ' Select the current heading in the document including
  ' all subheadings and body text


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.

Selecting a heading

We can select words, lines, or paragraphs using keyboard shortcuts; but there is no stock way to select a heading and its content other than manually using the mouse or keyboard.

There are a few ways to do this in a macro, probably using Selection.Goto commands along with some variables; but those methods are unnecessarily complicated.

A better quick one-line command exists if we instead take advantage of a hidden bookmark Word defines for every document. If you’re interested in other predefined bookmarks for your reading pleasure check the Microsoft help page.

A bookmark? Huh?

Intuitively, you probably think of a bookmark as just a location in a document since that’s how we physically use them in a real book, but in Word, a bookmark includes a Range of content (among other information).

If it helps the mental analogy, you might think of a real bookmark as marking both pages of content rather than just a specific location.

Ranges have a specific meaning in VBA, and we’ve covered them more detail. For our purposes now, a Range stores its own Start and End positions in the document which we can manipulate.

The resulting command to select our heading is not as obvious as some we’ve covered in past newsletters, but it’s better. One line and more versatile than doing it by a more obvious, direct way.

Bookmarks and document Ranges

How can we get the heading range using a bookmark?

Word stores all bookmarks in Bookmarks collection of the Selection.

  Selection.Bookmarks("\HeadingLevel")

Technically the Bookmarks collection will contain only those contained within the currently selected text, but this doesn't affect our macro here.

A backslash \ is the first character for all the standard hidden Word bookmarks.

We include the bookmark name in double quotes inside parentheses. The parentheses are needed since we’re going to do something with the bookmark below. The double quotes are needed since we have to give the name of the bookmark we’re accessing.

Now we just have to select the Range corresponding to the pre-defined Word Bookmark. As mentioned, a Bookmark is essentially a Range in the document, so we just select it directly without having to use any intermediate steps:

  Selection.Bookmarks("\HeadingLevel").Select

Works for derived headings

The best thing here is this works even for derived style headings not just the standard Word styles Heading 1 – 9.

I happen to include a lot of outline (in progress) headings like Act, Chapter, Scene, Subscene, etc. as I write (allows me to clearly see the progression of my story as it develops), so I use a lot of derived headings in my novels and notes.

When I get ready to publish or pass my text along to a beta reader, I just use a macro to strip all the heading related paragraphs (other than chapters) from my document.

Final Main Macro

The final macro is wonderfully simple:

Sub SelectHeadingContent()
  ' Select the current heading in the document including
  ' all subheadings and body text

  Selection.Bookmarks("\HeadingLevel").Select
End Sub

I use this more when editing blog or newsletter content but also in novel notes. I don’t need it all the time, but it’s definitely handy when I do especially when the heading content extends off the current page.

Your insertion point can start at any location within the heading level, and it still selects all the heading content.

Just to be clear, it selects all subordinate content, even subheadings under your current heading level.

I assigned this macro to Command+Option+Numpad5 in Word for Mac or Control+Alt+Numpad5 in Word for Windows. I know these shortcuts are a little odd, but I wanted something relatively easy to remember while not conflicting with any other keyboard shortcuts.

Variations

Upcoming blog articles will cover other extensions I’ve implemented, like selecting the remainder of the heading text, expanding or extending which heading is selected, and more.

A few times, I’ve wanted to select the body text under the heading but not the heading paragraph itself.

It’s slightly more complicated but still only two macro steps.

That’s one of the nice things about macros. We can mold them to make Word work our way.

Selecting only the heading content

If you happen to only want the subordinate text and exclude the main heading paragraph, you can strip away the first paragraph (the main heading) at the beginning of the bookmark range.

It’s easier than it sounds.

Where is the second paragraph?

We want to exclude the first heading paragraph which means we need to know when it ends, so we can reset the start of the selected heading text.

We’ve previously accessed the First paragraph Range using:

  Selection.Paragraphs.First.Range

But we don’t want the first paragraph. We want the beginning of the second paragraph. We actually want the End of the first paragraph Range.

  Selection.Paragraphs.First.Range.End

But … I want the second paragraph!

For those curious or questioning the solution above, the End of the Selection’s first paragraph is the exact same position in the document as the Start of the second paragraph.

Makes sense once you think about it a bit.

If that still bothers you, you can refer to the beginning of the second paragraph using an index of 2 in parentheses on the Paragraphs collection like so:

  Selection.Paragraphs(2).Range.Start

Not as clean as just referring to the End of the First paragraph, but it works.

Adjusting the Selection Start

Now we adjust the Start position of the Selection to exclude the heading paragraph.

The catch here is we’ve already selected the heading range using the standard Bookmark \HeadingLevel, so now we’re changing the Selection’s Start position instead.

  Selection.Start = Selection.Paragraphs.First.Range.End

So we only needed one extra macro step, but it did require a little more understanding of Word and Ranges to accomplish accurately.

Final macro variant

Just in case you don’t quite feel confident enough yet to assemble the pieces, here is the revised final macro.

Sub SelectHeadingContentWithoutHeading()
  ' Select the current heading in the document including
  ' all subheadings and body text

  Selection.Bookmarks("\HeadingLevel").Select
  Selection.Start = Selection.Paragraphs.First.Range.End
End Sub

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

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.