camelCase doesn't work for lowercaseUppercase words

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

camelCase doesn't work for lowercaseUppercase words

Post by Robie »

Hi

I am replacing all CamelCase characters in my documents with Courier New font, etc. But, I find that it doesn't find words like 'camelCase' (which starts with lowercase letter) but find everything beginning with uppercase, e.g. CamelCase.
Any ideas?

Code: Select all

Sub SetCamelCase()
    ' Handle simple form of CamelCase
    
    Application.ScreenUpdating = False
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "[A-Z][a-z]@[A-Z][a-z]@"
        .Replacement.Text = "^&"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
    End With
    While (Selection.Find.Execute)
        Selection.Start = SetToCourierNew
    Wend
    Application.ScreenUpdating = True
End Sub

Private Function SetToCourierNew() As Long
    Dim strDelim As String
    
    On Error GoTo ExitHandler
    ' Delimiters
    strDelim = " " & vbCr & vbTab & vbLf & Chr(11)
    With Selection
        ' Select whole word
        .MoveStartUntil strDelim, wdBackward
        .MoveEndUntil strDelim, wdForward
        ' Set font
        .Font.Name = "Courier New"
        ' Make sure this is not checked for spelling
        .NoProofing = True
    End With
    SetToCourierNew = Selection.End + 1
ExitHandler:
End Function
Thanks.
Robie.

User avatar
HansV
Administrator
Posts: 78851
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: camelCase doesn't work for lowercaseUppercase words

Post by HansV »

"[A-Z][a-z]@[A-Z][a-z]@" means: start with an upper case letter, then any number of lower case letters, then an upper case letter again, and finally any number of lower case letters. So you explicitly specify that the word has to start with an upper case letter. If you want to format both camelCase and CamelCase, use "[A-Za-z][a-z]@[A-Z][a-z]@":

Code: Select all

Sub SetCamelCase()
    ' Handle simple form of CamelCase
    Application.ScreenUpdating = False
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "[A-Za-z][a-z]@[A-Z][a-z]@"
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = True
        Do While .Execute
            Selection.Start = SetToCourierNew
        Loop
    End With
    Application.ScreenUpdating = True
End Sub
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: camelCase doesn't work for lowercaseUppercase words

Post by Robie »

HansV wrote:"[A-Z][a-z]@[A-Z][a-z]@" means: start with an upper case letter, then any number of lower case letters, then an upper case letter again, and finally any number of lower case letters. So you explicitly specify that the word has to start with an upper case letter. If you want to format both camelCase and CamelCase, use "[A-Za-z][a-z]@[A-Z][a-z]@":

Code: Select all

Sub SetCamelCase()
    ' Handle simple form of CamelCase
    Application.ScreenUpdating = False
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "[A-Za-z][a-z]@[A-Z][a-z]@"
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = True
        Do While .Execute
            Selection.Start = SetToCourierNew
        Loop
    End With
    Application.ScreenUpdating = True
End Sub
Thank you Hans. I knew I was missing something simple. CHEERS. :clapping: :fanfare: