Create a macro that searches for a case sensitive word and then changes the case for every word following it until another key character is found.

Hi All,

I would like to create a macro that will search the contents of a file to change the case of all words that fall between two sets of quotes following a keyword that is case sensitive.

 

First example: Here is my current test:

Label “THIS IS A TEST”

I would like search for the case sensitive literal “Label “” (Label-space-quote) and make sure that every word encased in the quotes retain the only first letter of each as a capital letter. The output should be:

                Label “This Is A Test”

 

 Second Example:

                Label “This is not a Test”

I would like search for the case sensitive literal “LABEL “” (LABEL-space-quote) and make sure that every word from the first quote to the second quote in the string is all capitalized. The output should be:

                LABEL “THIS IS NOT A TEST”  

Any help you can provide would be great!!!

Thanks in advance,


Wayne                                                    

Answer
Answer

I think this macro will do what you're asking for.

Sub LabelCase()

    Dim rgSrch As Range
    Dim rgChg As Range
    Dim strLabel As String

    Set rgSrch = ActiveDocument.Range

    With rgSrch.Find
        .Text = "label " & Chr(34)
        .MatchCase = False
        While .Execute
            Set rgChg = rgSrch.Duplicate
            With rgChg
                .Collapse wdCollapseEnd
                ' extend range to next quote or paragraph mark,
                ' whichever comes first
                .MoveEndUntil Cset:=Chr(34) & Chr(13), Count:=wdForward
            End With

            Select Case Left(rgSrch.Text, 2)
                Case "La"   ' want title case
                    ' force change of all caps in mixed-case title
                    rgChg.Case = wdLowerCase
                    rgChg.Case = wdTitleWord
                Case "LA"   ' want all caps
                    rgChg.Case = wdUpperCase
                Case "la"   ' want all lower case
                    rgChg.Case = wdLowerCase
                Case Else   ' mistake -- do nothing
            End Select

            rgSrch.Collapse wdCollapseEnd
        Wend
    End With
End Sub

There are a couple of items to note:

  • If the label that you want to change is missing the ending quote mark, a naive treatment would consider everything from the opening quote up to the quote after the next "Label" marker to be one big label. I've handled this by assuming that in such a case, the current label ends at the next paragraph mark.
  • An oddity of the way Word applies the wdTitleWord case is that if the text in the range includes some words in all caps and other words in lower case or mixed case, then the all-caps words are left all-caps. To work around this, I've first changed the text to all-lower-case and then applied wdTitleWord.
_____________________________
https://jay-freedman.info

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 735 Applies to: