Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Searching for Borders.

Searching for Borders

Written by Allen Wyatt (last updated November 28, 2020)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


2

Word has a very powerful feature that allows you to search for just about anything under the sun. One thing you cannot search for is borders on paragraphs, however. For instance, if you wanted to find all paragraphs that had the left border turned on, you can't do it. There are a couple of ways to work around this, however.

The first workaround is to simply use styles to format your paragraphs. If you use a style, and the style calls for a left-side border on a paragraph, then you can easily search for paragraphs using that style. (Exactly how you search for styles has been covered in other issues of WordTips.)

The second possible workaround is to do your searching using a macro. Using a macro, you can easily check to see if any border attributes are set for a paragraph. The following macro steps through each of the paragraphs in a document. When it finds a paragraph that has any of the border attributes set, that paragraph is selected, and the macro stops.

Sub SearchForBorders1()
    Dim k As Word.Paragraph
    Dim bFound As Boolean

    For Each k In ActiveDocument.Paragraphs
        bFound = False
        If k.Borders(wdBorderTop).LineStyle <> wdLineStyleNone _
          Then bFound = True
        If k.Borders(wdBorderLeft).LineStyle <> wdLineStyleNone _
          Then bFound = True
        If k.Borders(wdBorderBottom).LineStyle <> wdLineStyleNone _
          Then bFound = True
        If k.Borders(wdBorderRight).LineStyle <> wdLineStyleNone _
          Then bFound = True

        If bFound Then
            k.Range.Select
            Exit Sub
        End If
    Next k
End Sub

This macro can be very handy if you don't have many paragraphs with borders. Why? Because the macro always begins searching from the start of the document, and therefore will only find the first paragraph with a border set.

A different macro approach can be used to search for borders in paragraphs beyond the one in which the insertion point is located. The following macro does just that—it starts searching after the current paragraph and stops when it reaches a paragraph that has any of its border attributes set. Note that this macro doesn't select the entire paragraph; it simply moves the insertion point to the paragraph that has a border set.

Sub SearchForBorders2()
    Static a As Long, l As Long
    Dim b As Boolean
    Dim bd As Border
    Dim bds As Borders
    Dim prg As Paragraph
    Dim prgs As Paragraphs
    Dim re As Range
    Dim se As Selection
    Dim doc As Word.Document

    Set se = Selection
    Set re = se.Range
    Set doc = ActiveDocument
    If se.Start < l Then a = se.Start
    With doc.Content
        Set bds = .Borders
        re.Start = a
        re.End = .End
    End With
    For Each bd In bds
        b = bd = True
        If b Then Exit For
    Next
    If Not b Then Exit Sub

    Set prgs = re.Paragraphs
    For Each prg In prgs
        Set re = prg.Range
        If InStr(re.Text, Chr(13)) = 0 Then
            re.End = re.End + 1
        End If
        Set bds = re.Borders
        For Each bd In bds
            b = bd = True
            If b Then Exit For
        Next
        If b Then
            a = re.Start
            se.Start = re.Start
            se.End = re.Start
            a = re.End
            l = se.Start
            Exit Sub
        End If
    Next
    a = 0
    MsgBox "No more borders found"
End Sub

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (9833) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word here: Searching for Borders.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Quickly Updating Values

You can easily adjust the values in a range of cells by a certain amount. The key is to modify how you use the pasting ...

Discover More

Finding the Address of the Lowest Value in a Range

Uncovering the lowest value in a range is relatively easy; you can just use the MIN worksheet function. Discovering the ...

Discover More

Changing Gridline Color

Gridlines are very helpful in seeing where cells are located on the screen. You are not limited to black gridlines; ...

Discover More

The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2013. Spend more time working and less time trying to figure it all out! Check out Word 2013 For Dummies today!

More WordTips (ribbon)

Searching for Character Formatting

Need to look for a piece of text possessing a particular formatting attribute? Here's the skinny on how this is accomplished.

Discover More

Searching for Non-Black Text

Searching for text having (or not having) specific formatting is generally pretty easy. It is more difficult to search ...

Discover More

Searching for Periods Not Followed by a Space

Most periods should be followed by at least one space. What if you think there may be some errors in how your post-period ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 + 5?

2021-02-18 11:21:38

Joy Freeman

I was very excited to find this. In a document with multiple bordered paragraphs scattered throughout, if I just run the first macro over and over, each time removing the border formatting, I can work my way through a document. But what I'd really like to do is start the macro and it find each bordered paragraph and highlight each one. Then I can find all highlighted paragraphs and decide how to deal with each.

I thought you were saying the second one would at least cycle through the document finding each instance, but I don't know how to get it to work. I tried pasting a unique bracketed string (such as <here>, for later searching) when the insertion point was moved to the next bordered paragraph, but I can't figure out how the macro is supposed to be resumed to find the next, so I had to just run it over and over.

I'm obviously missing something. I actually use macros quite a bit, and have tweaked some to fit my needs, but I'm no coder. Can you advise me?

Thank you!


2020-11-29 02:39:51

Oswald Cornillie

Selecting a word: to select additional words, hold down the control button (not the mouse button)...


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.