Simpatie.ro - matrimoniale
Mrrrr's Forum (VIEW ONLY)
Un forum care ofera solutii pentru unele probleme legate in general de PC. Pe langa solutii, aici puteti gasi si alte lucruri interesante // A forum that offers solutions to some PC related issues. Besides these, here you can find more interesting stuff.
Nou pe simpatie:
roxigirl85 pe Simpatie.ro
Femeie
25 ani
Bucuresti
cauta Barbat
30 - 46 ani
Mrrrr's Forum (VIEW ONLY)ReguliInregistrareLoginPozeNu sunteti logat. Lista Forumurilor Pe Tematici
Mrrrr's Forum (VIEW ONLY) / Tutoriale si Ghiduri Utile // Tutorials and useful guides /

[WORD] Find & Replace and Highlight Replacements [VBA]

Pagini: 1 Moderat de TRaP, TonyTzu
#1
TRaP
Moderator
Postari: 960
Source:
https://superuser.com/questions/1299220 ... at-words-w

Note: The code below automatically highlights the text in the current highlight color (since default is yellow, it will do yellow). However, if you change the color from the highlight tool on Home tab before running the code, it will highlight the text in that color.


Sub Replace_and_Highlight()

findArray = Array("GSI") 'Array("Red 1", "Blue 1", "Green 1")
replArray = Array("Great System Information") 'Array("Red 2", "Blue 2", "Green 2")

For i = 0 To UBound(findArray)
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Highlight = True

    With Selection.Find
        .Text = findArray(i)
        .Replacement.Text = replArray(i)
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With

Selection.Find.Execute Replace:=wdReplaceAll
Next i

End Sub


 
   
#2
TRaP
Moderator
Postari: 960
To have the color always switched to the one you desire inside the code, and not have to always select it before running the code, you can put one of the following lines before line Selection.Find.Replacement.Highlight = True

    Options.DefaultHighlightColorIndex = wdBrightGreen
    Options.DefaultHighlightColorIndex = wdTurquoise
    Options.DefaultHighlightColorIndex = wdPink
    Options.DefaultHighlightColorIndex = wdBlue
    Options.DefaultHighlightColorIndex = wdRed
    Options.DefaultHighlightColorIndex = wdDarkBlue
    Options.DefaultHighlightColorIndex = wdTeal
    Options.DefaultHighlightColorIndex = wdGreen
    Options.DefaultHighlightColorIndex = wdViolet
    Options.DefaultHighlightColorIndex = wdDarkRed
    Options.DefaultHighlightColorIndex = wdDarkYellow
    Options.DefaultHighlightColorIndex = wdGray50
    Options.DefaultHighlightColorIndex = wdGray25
    Options.DefaultHighlightColorIndex = wdBlack
    Options.DefaultHighlightColorIndex = wdNoHighlight


 
   
#3
TRaP
Moderator
Postari: 960
To insert words via InputBox, separated by commas, see source: http://www.msofficeforums.com/word-vba/ ... -user.html

The code from post 1 updated with input boxes for find terms and replace terms is below:


Sub Replace_and_Highlight()

Dim findArray, replArray, i As Long

findArray = InputBox(prompt:="Put list of terms to be replaced, separated by commas", Title:="What to find?", Default:="")
replArray = InputBox(prompt:="Put list of terms to replace with, separated by commas", Title:="What to replace with?", Default:="")

For i = 0 To UBound(Split(findArray, ","))
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
'    Options.DefaultHighlightColorIndex = wdBrightGreen
'    Options.DefaultHighlightColorIndex = wdTurquoise
'    Options.DefaultHighlightColorIndex = wdPink
'    Options.DefaultHighlightColorIndex = wdBlue
'    Options.DefaultHighlightColorIndex = wdRed
'    Options.DefaultHighlightColorIndex = wdDarkBlue
'    Options.DefaultHighlightColorIndex = wdTeal
'    Options.DefaultHighlightColorIndex = wdGreen
'    Options.DefaultHighlightColorIndex = wdViolet
'    Options.DefaultHighlightColorIndex = wdDarkRed
'    Options.DefaultHighlightColorIndex = wdDarkYellow
'    Options.DefaultHighlightColorIndex = wdGray50
'    Options.DefaultHighlightColorIndex = wdGray25
'    Options.DefaultHighlightColorIndex = wdBlack
'    Options.DefaultHighlightColorIndex = wdNoHighlight
    Selection.Find.Replacement.Highlight = True

    With Selection.Find
        .Text = Split(findArray, ",")(i)
        .Replacement.Text = Split(replArray, ",")(i)
        .Forward = True
        .Wrap = wdFindContinue 'wdFindStop
        .Format = True
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With

Selection.Find.Execute Replace:=wdReplaceAll
Next i

End Sub


 
   
Pagini: 1  
Mergi la