Change word macro to scan selected text only (to save time in large documents)

140 Views Asked by At

I used the macro below and it works nicely! However, would it be possible to limit it to the text I selected in the active document only? The seperate document has 2000 words, so if the active document has too much text it will take ages... Even though I might only want to scan like 100 words to be highlighted... If anyone can help, much appreciated!

Sub B2_Highlighter_Selection_andAllWordForms()
    Application.ScreenUpdating = False
    Options.DefaultHighlightColorIndex = wdBrightGreen
    Dim FRDoc As Document, FRList, i As Long
    Set FRDoc = Documents.Open("C:\Users\Milan van Bree\Google Drive\Heerbeeck College\3. Extra materiaal algemeen\Macros for Word\B2words.docx", ReadOnly:=True, Addtorecentfiles:=False, Visible:=False)
    FRList = FRDoc.Range.Text: FRDoc.Close False: Set FRDoc = Nothing
    With Selection.Range.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .MatchWholeWord = True
        .MatchCase = False
        .Replacement.Text = "^&"
        .Replacement.Highlight = True
        'Process each word from the List
        For i = 0 To UBound(Split(FRList, vbCr)) - 1
            .Text = Split(FRList, vbCr)(i)
            .Execute Replace:=wdReplaceAll
        Next
    End With
    Application.ScreenUpdating = True
End Sub

I tried to google it and I did find some answers, but that didnt compute with my specific macro type that highlights words that are stored in a seperate document.

1

There are 1 best solutions below

1
macropod On

After:

.Replacement.Highlight = True

Insert:

.Wrap = wdFindStop