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:
- Open the Macro Editor :
- Go to
Tools > Macros > Organize Macros > Basic >
My Macros > Standard > TitleCaseModule > TitleCase
- Click
Edit
.
- 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
-
Save the Macro : After pasting the corrected code, click on the save icon or go to
File > Save
. - 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