2

I'm hoping for a macro code that will work on WORD 2003. I have text like the following:

THIS IS THE BEGINNING of a sentence that continues on from here.

I'd like to make the capitalized text bold:

THIS IS THE BEGINNING of a sentence that continues on from here.

I understand that this would be trivial if the bold text were text-formatted as "AllCaps" but it is not. It was created using caps lock.

Any ideas? I can get it going, but I can't figure out how to add a complex find and replace within the VBA code.

braX
  • 11,506
  • 5
  • 20
  • 33
user1132149
  • 21
  • 1
  • 3

1 Answers1

5

What you want is Regular Expressions, which Word provides in the advanced section of its Find functionality.

Here's a quick sample:

Public Sub Test()
    With ActiveDocument.Content.Find
        .Text = "<[A-Z]{1,}>"
        .MatchWildcards = True
        .MatchCase = True
        .Replacement.Font.Bold = True
        .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
    End With
End Sub

"<[A-Z]{1,}>" means the following:

  • "<" means match starting at the beginning of a word
  • "[]" means match one of the characters inside the brackets
  • "[A-Z]" the A-Z part, when inside the brackets, means the uppercase letters A through Z
  • "{1,}" means match the preceding at least once (in this case, an uppercase letter must occur at least once in a row)
  • ">" means the match must end at the end of a word. The pattern would not match, for example "THIs" because the uppercase letters do not go to the end of the word.

One caveat you have to deal with is single-letter words - I and A will be bolded with this pattern scheme. You may find it easier to create a pattern that matches whole groups of uppercase words, but you will still have to find a way to deal with leading or trailing uppercase, single-letter words.

If you have questions, leave a comment and I can try to explain further.

Blackhawk
  • 5,984
  • 4
  • 27
  • 56