Page (that is, section) borders

Section borders

  1. Enable borders for one section, and they are enabled for the whole document.
  2. Control which pages in a section display borders using .EnableFirstPage and .EnableOtherPages.
  3. Set the .LineStyle, .LineWidth and .Color for each section's individual borders (top, right, bottom, left).

Both Word's user interface and the object model documentation refer to 'page borders'. In fact, there's no such thing. What does exist are borders for sections.

This page describes my experience managing so-called 'page borders'.

Overview

There is no property of a Document that lets you control the visibility of page (that is, section) borders. We control section borders at the level of the Section.

Each section can have 4 borders: top, right, bottom, left. There are no horizontal, vertical or diagonal borders for a section. Trying to access horizontal, vertical or diagonal borders for a section throws error 5941 'The requested member of the collection does not exist'.

There is one set of borders for each section. It is not possible to have, say, pink borders on page 1 and green borders on page 2 of a section. However, that one set of borders may display on the first page only of a section, or on second and subsequent pages, or both.

At the document level: Enabling borders with Sections(n).Borders.Enable

The Section object has a .Borders.Enable property. You can set this property to True or False. Sadly, however, it does not confine itself to one section. No matter which of your document's Sections you apply this to, Word will change the value of the property for all sections in the document.

Therefore, if we want any section borders, this property must be True. Once you've set it to True, don't change it or you'll need to re-set all the details of individual borders in individual section.

While we can't set the .Enable property for an individual section, we can read it for each individual section. The possible return values for Sections(n).Enable appear to be:

  • If all four borders in the section have .LineStyle = wdLineStyleNone, then Word returns 0 (that is, False)
  • If all four borders in the section have .LineStyle other than wdLineStyleNone, then Word returns -1 (that is, True)
  • Else, Word returns wdUndefined (9999999).

For what it's worth, the object model help does not mention wdUndefined as a possible valid value here.

Where will the border appear: Sections(n).Borders.EnableFirstPage and .EnableOtherPages

Turning borders on

We can control which borders are displayed within a section by using .EnableFirstPage and/or .EnableOtherPages.

But we are not entirely in control. If you set, for example,

ActiveDocument.Sections(1).Borders.Enable = True

then Word will ensure that either .EnableFirstPage or .EnableOtherPages is True. For example:

  • .EnableFirstPage has previously been set to False.
  • Your code sets .EnableOtherPages = False.
  • Word will set .EnableFirstPage to True.

That is, if ActiveDocument.Sections(1).Borders.Enable is True, then Word will not allow both .EnableFirstPage and .EnableOtherPages to be False. It is, however, OK to have them both True.

Turning borders off

.EnableFirstPage and .EnableOtherPages are useful for showing borders, but not useful for not showing borders.

Therefore, if we don't want borders, we must turn each off at the level of the individual border.

And we can do that in one of two ways: by setting the .Visible property of the border, or setting the border's .LineStyle to wdLineStyleNone.

For example, in VBA, you can turn off the top border of section 2 using:

ActiveDocument.Sections(2).Borders(wdBorderTop).Visible = False, or

ActiveDocument.Sections(2).Borders(wdBorderTop).LineStyle = wdLineStyleNone

It doesn't matter which of these our code does. Word will do the other. That is, if we set .Visible to False, then Word will change the .LineStyle to none. And vice versa.

Summary of what borders you see

This table shows what section borders you will see for different settings of Sections(n).Enable, .EnablePage1, .EnableOtherPages and individual borders' .LineStyle.

.Enable .EnablePage1 .EnableOtherPages .LineStyle See on p1? See on other pages?

False

True or False

True or False

Any

No

No

True

True or False

True or False

wdLineStyleNone

No

No

True

True

False

Anything except wdLineStyleNone

Yes

No

True

False

True

Anything except wdLineStyleNone

No

Yes

True

True

True

Anything except wdLineStyleNone

Yes

Yes

Controlling individual borders

You can set the line style, line width and colour for each border individually, as described at Border Basics for Developers.

There are, however, some curiosities.

According to the documentation, a border .LineWidth can be either wdUndefined (9999999) or one of the wdLineWidth constants. But:

  • In Word 2003, a section's default border .LineWidth equals -2098703536. That number is not valid, according to the documentation. I don't know what it means.
  • In Word 2007, the default .LineWidth equals 7405540. That number is not valid, according to the documentation. I don't know what it means.
  • In Word 2010, the default is 2 (which is wdLineWidth025pt).

(I don't make this stuff up, you know!)

In all versions of Word that I've checked, the default for section borders is that .Enable = False. So in a way, it doesn't matter what the line width is, because if .Enable = False, we can't see any borders.

Step by step guide to managing section borders in code

If I want to manage any section borders in code, this is what I find I need to do:

  1. Turn on all the borders in the document by setting Section(1).Borders.Enable = True. This will turn on borders for all sections. It does not constrain itself to Section (1).
  2. Cycle through each section, and:
    1. Set either or both of .EnableFirstPage or .EnableOtherPages to True, but at no time allow both to be False.
    2. Cycle through each of the 4 borders in the section and:
      1. Set each individual border's .LineStyle either to wdLineStyleNone or to the line style I want (eg ActiveDocument.Sections(2).Borders(wdBorderTop).LineStyle = wdLineStyleDot)
      2. If (and only if!) .LineStyle is not wdLineStyleNone, then set the .LineWidth and .Color of the border. Remember that not every .LineWidth is available for every .LineStyle.
      3. Set the section's .Borders.DistanceFrom property to identify whether distances are measured from the border out from the text or in from the paper edge.
      4. Set the section's .Borders.DistanceFromTop, .DistanceFromLeft etc properties.
      5. Set the other properties that are applicable only to section borders (.AlwaysInFront, .JoinBorders, .SurroundFooter and .SurroundHeader).
      6. Don't bother setting the .Shadow. It doesn't work, but it doesn't cause an error, either.