One-click 'Title Case' for highligthed text

Here’s a version of a macro that includes a list of common conjunctions and prepositions to be lowercased unless they are the first word of the sentence.

If any of the following gives you issues, I suggest to use a public available AI to solve it. After all, that’s how this one was made and modified.

To begin with you should make a macro (below I use for the module: TitleCaseModule and the macro itself: TitleCase). If needed, look up how to create a Standard macro in LibreOffice Writer. Once done simply followed the steps below:

  1. Open the Macro Editor :
  • Go to Tools > Macros > Organize Macros > Basic > My Macros > Standard > TitleCaseModule > TitleCase
  • Click Edit .
  1. Replace any Existing Code and copy the following :
       REM ***** BASIC *****
Sub TitleCase()
    Dim oDoc As Object
    Dim oText As Object
    Dim oCursor As Object
    Dim sText As String
    Dim aWords As Variant
    Dim i As Integer
    Dim sLowerCaseWords As String
    Dim aLowerCaseWords As Variant
    
    oDoc = ThisComponent
    oText = oDoc.Text
    oCursor = oDoc.CurrentController.getViewCursor()
    
    ' Get the selected text
    sText = oCursor.getString()
    
    ' Split the text into words
    aWords = Split(sText, " ")
    
    ' Define conjunctions and prepositions to be lowercased
    sLowerCaseWords = "and or but nor for so yet a an the in on at to of"
    aLowerCaseWords = Split(sLowerCaseWords, " ")
    
    ' Process each word
    For i = LBound(aWords) To UBound(aWords)
        If i = 0 Then
            ' Always capitalize the first word
            aWords(i) = UCase(Left(aWords(i), 1)) & LCase(Mid(aWords(i), 2))
        Else
            ' Check if the word is in the list of lower case words
            If IsInArray(LCase(aWords(i)), aLowerCaseWords) Then
                aWords(i) = LCase(aWords(i))
            Else
                aWords(i) = UCase(Left(aWords(i), 1)) & LCase(Mid(aWords(i), 2))
            End If
        End If
    Next i
    
    ' Set the modified string back to the cursor position
    oCursor.setString(Join(aWords, " "))
End Sub

Function IsInArray(sWord As String, aArray As Variant) As Boolean
    Dim i As Integer
    IsInArray = False
    For i = LBound(aArray) To UBound(aArray)
        If aArray(i) = sWord Then
            IsInArray = True
            Exit Function
        End If
    Next i
End Function
  1. Save the Macro : After pasting the corrected code, click on the save icon or go to File > Save .
  2. Close the Macro Editor : You can now close the macro editor.

Finally, you may want to make a button in your toolbar to directly run the macro on any selected text. In your TitleCaseModule will see both TitleCase and also IsInArray. Your button should point to the former.

In appreciation to everyone who have contributed to the wonderful LibreOffice :+1:

would be a good exercise to go with ReplaceDescriptor
searching and replacing text with regular expressions

No need for Macro… because ⇒ Format ⇒ Text ⇒ Capitalize Every Word exists.

You may assign a keyboard-shortcut? for example ctrl T:

⇒ Tools ⇒ customize ⇒

If IsInArray(LCase(aWords(i)), aLowerCaseWords) Then

Oh… my fault … but that is by definition NOT »Title Case«

@karolus I suppose it depends on what definition of ‘Title Case’ you accept. I’m going with this one from Wikipedia:

Title case or headline case is a style of capitalization used for rendering the titles of published works or works of art in English. When using title case, all words are capitalized, except for minor words (typically articles, short prepositions, and some conjunctions) that are not the first or last word of the title.

The Cambridge dictionary states the same:

The use of capital letters in the most important words of titles of books, films, etc.

“Capitalize every word” is what it says and does not refer to the specific “Title Case” - just check out the consensus summarized in the Wikipedia article I linked above