DALLASCAO.COM

Site of Cao Shouguang, English to Chinese translator

Word Macro VBA: Capitalize 1st letter of each line

Don’t miss out the translator’s productivity tool that can genuinely increase your translation speed and quality: GT4T!!
Check it out here: GT4T website

Convert:

My name is ABC
- I am from XYZ
-he works on a word document
1. hello there

to:

My name is abc
- I am from xyz
-He works on a word document
1. Hello there
Sub captalize()

' captalize the first letter of each line and uncaptitalize the rest


para_count = ActiveDocument.Paragraphs.Count
For i = 1 To para_count
    para_length = ActiveDocument.Paragraphs(i).Range.Characters.Count
    first_char = True
     For j = 1 To para_length
          Set current_char = ActiveDocument.Paragraphs(i).Range.Characters.Item(j)
          
          If IsLowerChar(current_char.Text) Then
            If first_char Then
                current_char.Select
                Selection.TypeText (UCase(current_char.Text))
            End If
            first_char = False
          ElseIf IsUpperChar(current_char.Text) Then
            If Not first_char Then
                current_char.Select
                Selection.TypeText (LCase(current_char.Text))
            End If
            first_char = False
          End If
    Next j
Next i
End Sub


Function IsLowerChar(CHR As String) As Boolean
    'by Rolf Keller
    CHR = Left(CHR, 1)
    X1 = (LCase(CHR) = CHR)
    X2 = (UCase(CHR) = CHR)
    IsLowerChar = X1 And (Not X2)
End Function

Function IsUpperChar(CHR As String) As Boolean
    'by Rolf Keller
    CHR = Left(CHR, 1)
    X1 = (LCase(CHR) = CHR)
    X2 = (UCase(CHR) = CHR)
    IsUpperChar = (Not X1) And (X2)
End Function

Leave a Comment