locked
Find & bold all instances of selected text using delimiter RRS feed

  • Question

  • Hi there!

    I am trying to write a pretty simple macro, but it's currently got me pulling my hair out.

    I have a document in which I would like to bold all instances of titles/keywords.

    The titles/keywords are listed at the beginning of the document using " | " as a delimiter.

    I want the user to be able to select the title/keyword list and run the macro to bold all instances of each individual title/keyword.

    Here's the code:

    Sub BoldSelected()
    ' Find all isntances of selected text and bold it
        Selection.Find.ClearFormatting
        
        'Dim variables
        Dim keywordArray() As String
        Dim i As Integer
        
        'Split selected text into array elements before and after " | "
        keywordArray() = Split(Word.Selection, " | ")
            
        'Loop through array
        For i = 0 To UBound(keywordArray)
             
            'Delete the last characters if they are a space or a return
            If keywordArray(i).Characters.Last = Chr(32) Or keywordArray(i).Characters.Last = Chr(13) Then
                keywordArray(i).End = keywordArray(i).End - 1
            End If
        
            'find all instances of the array element and select
            With Selection.Find
                .Text = keywordArray(i)
                .Forward = True
                .Wrap = wdFindStop
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .IgnorePunct = True
                .IgnoreSpace = True
            End With
            
            Selection.Find.Execute
            
            'loop until no more instances are found
            If Selection.Find.Found Then
                While Selection.Find.Found
                    Selection.Font.Bold = True
                    Selection.Find.Execute
                Wend
                
            Else
                'display error message if nothing found initially
                MsgBox (keywordArray(1) & "not found in this document")
            End If
        
        Next i 'do next array element
        
    End Sub

    I know I'm missing something obvious, but I have stared at this too long and can't find my errors...

    I am getting errors about the Array in the For loop which leads me to believe maybe I'm not Dimensioning the array correctly?

    Any input is appreciated.


    Thanks!

    Thursday, January 17, 2013 9:30 PM

Answers

  • The elements of the keyword array are strings, not ranges, so they don't have methods such as Characters or End.

    You can specify bold in the Replacement property of the Find object, and bold all found instances in one go, without looping.

    You have to jump back to the beginning of the document for each keyword.

    Sub BoldSelected()
        ' Find all isntances of selected text and bold it
        ' Dim variables
        Dim keywordArray() As String
        Dim i As Integer
    
        ' Split selected text into array elements before and after " | "
        keywordArray() = Split(Word.Selection, " | ")
    
        ' Loop through array
        For i = 0 To UBound(keywordArray)
            'Delete the last characters if they are a space or a return
            If Right(keywordArray(i), 1) = " " Or Right(keywordArray(i), 1) = vbCr Then
                keywordArray(i) = Left(keywordArray(i), Len(keywordArray(i)) - 1)
            End If
            Selection.HomeKey Unit:=wdStory
            ' find all instances of the array element and select
            With Selection.Find
                .ClearFormatting
                .Text = keywordArray(i)
                .Replacement.ClearFormatting
                .Replacement.Text = "^&"
                .Replacement.Font.Bold = True
                .Forward = True
                .Wrap = wdFindStop
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .IgnorePunct = True
                .IgnoreSpace = True
                .Execute Replace:=wdReplaceAll
            End With
        Next i ' do next array element
    End Sub


    Regards, Hans Vogelaar

    • Marked as answer by Jake Harris Thursday, January 17, 2013 11:15 PM
    Thursday, January 17, 2013 10:20 PM

All replies

  • The elements of the keyword array are strings, not ranges, so they don't have methods such as Characters or End.

    You can specify bold in the Replacement property of the Find object, and bold all found instances in one go, without looping.

    You have to jump back to the beginning of the document for each keyword.

    Sub BoldSelected()
        ' Find all isntances of selected text and bold it
        ' Dim variables
        Dim keywordArray() As String
        Dim i As Integer
    
        ' Split selected text into array elements before and after " | "
        keywordArray() = Split(Word.Selection, " | ")
    
        ' Loop through array
        For i = 0 To UBound(keywordArray)
            'Delete the last characters if they are a space or a return
            If Right(keywordArray(i), 1) = " " Or Right(keywordArray(i), 1) = vbCr Then
                keywordArray(i) = Left(keywordArray(i), Len(keywordArray(i)) - 1)
            End If
            Selection.HomeKey Unit:=wdStory
            ' find all instances of the array element and select
            With Selection.Find
                .ClearFormatting
                .Text = keywordArray(i)
                .Replacement.ClearFormatting
                .Replacement.Text = "^&"
                .Replacement.Font.Bold = True
                .Forward = True
                .Wrap = wdFindStop
                .Format = True
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .IgnorePunct = True
                .IgnoreSpace = True
                .Execute Replace:=wdReplaceAll
            End With
        Next i ' do next array element
    End Sub


    Regards, Hans Vogelaar

    • Marked as answer by Jake Harris Thursday, January 17, 2013 11:15 PM
    Thursday, January 17, 2013 10:20 PM
  • Ahh thank you Hans, works like a charm!

    I had previously toyed with the idea of using the replace function, but I was unable to find a way to do it while retaining capitalization. That's what led me to the bloated loop I provided above. This is a far more elegant solution.

    Can you exlpain the following line for me? I'm not sure what the "^&" does.

    .Replacement.Text = "^&"

    Thursday, January 17, 2013 11:15 PM
  • ^& is the code for "whatever has been entered in the Find what box".

    When you activate the Replace dialog in Word itself, click in the Replace with box, then click Special, you'll see a list of special items that can be entered in the Replace with box. If you select "Find What Text", ^& will be entered in the Replace with box.


    Regards, Hans Vogelaar

    Thursday, January 17, 2013 11:36 PM