Word.Tips.Net WordTips (Menu Interface)

Searching for Borders

Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Searching for Borders.

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
            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
    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
        If b Then
            a = re.Start
            se.Start = re.Start
            se.End = re.Start
            a = re.End
            l = se.Start
            Exit Sub
        End If
    a = 0
    MsgBox "No more borders found"
End Sub

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (1705) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Searching for Borders.

Related Tips:

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!


Leave your own comment:

  Notify me about new comments ONLY FOR THIS TIP
Notify me about new comments ANYWHERE ON THIS SITE
Hide my email address
*What is 5+3 (To prevent automated submissions and spam.)
           Commenting Terms

Comments for this tip:

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)

Our Company

Sharon Parq Associates, Inc.

About Tips.Net

Contact Us


Advertise with Us

Our Privacy Policy

Our Sites


Beauty and Style




DriveTips (Google Drive)

ExcelTips (Excel 97–2003)

ExcelTips (Excel 2007–2016)



Home Improvement

Money and Finances


Pests and Bugs

Pets and Animals

WindowsTips (Microsoft Windows)

WordTips (Word 97–2003)

WordTips (Word 2007–2016)

Our Products

Helpful E-books

Newsletter Archives


Excel Products

Word Products

Our Authors

Author Index

Write for Tips.Net

Copyright © 2016 Sharon Parq Associates, Inc.