Home > Enterprise >  Find Words with Specific Case and Replace in Word VBA
Find Words with Specific Case and Replace in Word VBA

Time:09-26

I am trying to find only words that are in all caps and replace them with a translated counterpart.

For example, I would like to replace the written number "ONE" with "UNO".

However, I am running into a problem that "one" appears in the foreign language in various words and is being replaced by "uno". I only need to replace the capitalized counterparts, is there a way to complete this?

This is the code I have been using;

    'ONE
            Selection.Find.ClearFormatting
            Selection.Find.Replacement.ClearFormatting
            With Selection.Find
                .Text = "ONE"
                .Replacement.Text = "UNO"
                .Forward = False
                .Wrap = wdFindAsk
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchByte = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
            End With
            Selection.Find.Execute Replace:=wdReplaceAll

A better option might be to only have it replace when it shows up as the whole word.

Edit: You just set .MatchWholeWord to true. -_- duh.

CodePudding user response:

To do this in bulk you might use code like:

Sub MultiFindReplace()
Application.ScreenUpdating = False
Dim FList As String, RList As String, i As Long
FList = "ZERO|ONE|TWO|THREE|FOUR|FIVE|SIX|SEVEN|EIGHT|NINE|TEN|ELEVEN|TWELVE"
RList = "CERO|UNO|DOS|TRES|CUATRO|CINCO|SEIS|SIETE|OCHO|NUEVE|DIEZ|ONCE|DOCE"
With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Format = False
  .Forward = True
  .MatchCase = True
  .MatchWholeWord = True
   'Process each item from the Find/Replace Lists
  For j = 0 To UBound(Split(FList, "|"))
    .Text = Split(FList, "|")(i)
    .Replacement.Text = Split(RList, "|")(i)
    .Execute Replace:=wdReplaceAll
  Next
End With
Application.ScreenUpdating = True
End Sub

CodePudding user response:

just set .MatchWholeWord to true!

  • Related