[Solved] How to make a sentence uppercase

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
smok2212
Posts: 18
Joined: Thu Mar 19, 2009 2:34 am

[Solved] How to make a sentence uppercase

Post by smok2212 »

Hi,
Is there any function or any other way to change a selected text to upper case?

Also, is there any way to change only the first letter of each word in a paragraph to uppercase?

Thanks

ps: Sorry for any mistake, English is not my first language.
Last edited by Hagar Delest on Mon Feb 14, 2011 11:56 am, edited 1 time in total.
Reason: tagged Solved.
OOo 3.0.X on Ms Windows XP
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

Re: How to make a sentence uppercase

Post by JohnV »

You can record Format > Change Case > Uppercase and Format > Character > Font Effects tab > Effect = Title or Effect = Uppercase.
smok2212
Posts: 18
Joined: Thu Mar 19, 2009 2:34 am

Re: How to make a sentence uppercase

Post by smok2212 »

Thanks for your help,

but I needto do that using OOo Basic language, can someone help me on that?

thanks
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to make a sentence uppercase

Post by FJCC »

OOoBasic has a UCase function that takes an input string and returns it with all characters in uppercase. This code takes a highlighted section of text and converts it to uppercase.

Code: Select all

Doc = ThisComponent
Selections = Doc.CurrentSelection
FirstSelection = Selections.getByIndex(0)
TextString = FirstSelection.String
UCaseStr = UCase(TextString)
FirstSelection.String = UCaseStr
I'll have to think about how to make the first letter of each word uppercase.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to make a sentence uppercase

Post by FJCC »

Here is some simple code that makes the first letter of each word in a selected range uppercase. I haven't thought about catching unusual cases. I just mean this as an illustration of a possible method.

Code: Select all

Doc = ThisComponent
oText = Doc.Text
Selections = Doc.CurrentSelection
FirstSelection = Selections.getByIndex(0)
Curs = oText.createTextCursorByRange(FirstSelection)
Curs.collapseToStart
Curs.gotoStartOfWord(False)
Comp = oText.compareRegionEnds(Curs.End,FirstSelection)
While Comp = 1
	Curs.goRight(1,True)
	Curs.String = UCase(Curs.String)
	Curs.gotoNextWord(False)
	Comp = oText.compareRegionEnds(Curs.End,FirstSelection)
Wend
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

Re: How to make a sentence uppercase

Post by JohnV »

FJCC,

Before you posted the code immediately above I was thinking about how I would approach this.
Here's what I came up with.

Code: Select all

Sub TitleCase
Doc = ThisComponent
oText = Doc.Text
Selections = Doc.CurrentSelection
FirstSelection = Selections.getByIndex(0)
aSplit = Split(FirstSelection.String)
For i = lBound(aSplit) to uBound(aSplit)
 st = aSplit(i)
 Mid(st,1,1,UCase(Mid(st,1,1))
 aSplit(i) = st
Next
FirstSelection.String = Join(aSplit) 
End Sub
Now can you or anyone else show me how this can be done using "For each" which,
as far as I know, is undocumented. Example:

Code: Select all

For each st in aSplit
 Mid(st,1,1,UCase(Mid(st,1,1))
 Print st
 'How do you get this element of the array to adopt the change in the string?
Next
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to make a sentence uppercase

Post by FJCC »

JohnV,
The only method I know for inserting back into the array is to include an index variable that increments with every loop

Code: Select all

i=0
For Each st in aSplit
	Mid(st,1,1,UCase(Mid(st,1,1))
	aSplit(i) = st
	i = i + 1
Next
Another approach is to construct the new string within the loop, rather than using the Join function.

Code: Select all

OldString = "A set of words that is long"
aSplit = Split(OldString)
NewString = ""
For Each st in aSplit
	Mid(st,1,1,UCase(Mid(st,1,1))
	If len(NewString) <> 0 then
		NewString = NewString + " " + st
	Else
		NewString = st
	End if
Next
That seems more in the spirit of the For Each method, though it requires the If statement to handle the first pass through the loop to avoid putting a space in front of the selected text. By the way, For Each is documented on page 39 the latest Basic Guide http://wiki.services.openoffice.org/w/i ... o3.1.0.pdf. It doesn't say much, but now it is an official feature. The guide also has the Type..End Type definition.

Finally, I think your method will not capitalize a word if it is proceeded by a parenthesis or quotation mark. The gotoNextWord method of a text cursor seems to handle those correctly
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
JohnV
Volunteer
Posts: 1585
Joined: Mon Oct 08, 2007 1:32 am
Location: Kentucky, USA

Re: How to make a sentence uppercase

Post by JohnV »

FJCC,
Thanks for the simple solution to "For each" and reference to its documentation.

Avoiding If, Else:

Code: Select all

 NewString = NewString + st + " "
Next
Print NewString,Len(NewString)
'Trim(NewString) REM Why doesn't this work??
NewString = Right(NewString,Len(NewString)-1)
Print NewString,Len(NewString)
Finally, I think your method will not capitalize a word if it is proceeded by a parenthesis or quotation mark.
Good catch and neither does the recorded version of Title Case.

Code: Select all

sub TitleCase
dim document   as object
dim dispatcher as object
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "CaseMap"
args1(0).Value = 3
dispatcher.executeDispatch(document, ".uno:CaseMap", "", 0, args1())
end sub
Perhaps you should provide your code as the basis of an Enhancement at OOo.
smok2212
Posts: 18
Joined: Thu Mar 19, 2009 2:34 am

Re: How to make a sentence uppercase

Post by smok2212 »

Thanks for the help guys
OOo 3.0.X on Ms Windows XP
rygle
Posts: 6
Joined: Wed Sep 09, 2009 9:47 am

Re: How to make a sentence uppercase

Post by rygle »

Can this be modified to to toggle between lower and upper case? I would love to have a single key that does just that function.
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to make a sentence uppercase

Post by FJCC »

I'm not sure exactly what you want to do. The code snippets are intended to change a selected range to uppercase or to make the first letter of each word in the selection uppercase. Do you want to invert the case of the selected text, make it uppercase if it is lowercase and make it lower case if it is uppercase, or do you want to do something else?
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
rygle
Posts: 6
Joined: Wed Sep 09, 2009 9:47 am

Re: How to make a sentence uppercase

Post by rygle »

Thanks for your reply. I would love to be able to toggle between uppercase and lowercase using a macro, and then to assign a keyboard shortcut to that - i.e. "text" -> "TEXT" -> "text". Also Text -> TEXT -> Text is preferable behaviour for a word with an existing initial capital. I can achieve this manually by highlighting some text and going to Format -> Character -> Font Effects -> Effects -> Capitals, and then revert by selecting (Without) instead of capitals.

I got in the habit of doing this using MS Word's CTRL-Shift-A to do this to give emphasis to words when preparing presentation notes. At present I have simply recorded a Macro for converting text to the uppercase text effect and another for removing the text effect, and then assigned these to CTRL-Shift-A and CTRL-Shift-Q, which is similar enough, but is proving difficult to relearn.

The macros I've recorded and have presently been using are as follows;

To convert to All Caps...

Code: Select all

sub All_Caps
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "CaseMap"
args1(0).Value = 1

dispatcher.executeDispatch(document, ".uno:CaseMap", "", 0, args1())


end sub
and to lose the All Caps...

Code: Select all

sub No_Caps
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(0) as new com.sun.star.beans.PropertyValue
args1(0).Name = "CaseMap"
args1(0).Value = 0

dispatcher.executeDispatch(document, ".uno:CaseMap", "", 0, args1())


end sub
Any help appreciated.

I also couldn't follow how to integrate all the above suggestions together to make a better Macro, which is why I'm stuck with the versions the Macro recorder makes.
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: How to make a sentence uppercase

Post by FJCC »

Here is some code that toggles text to be all uppercase or lowercase. I first tried to incorporate your code in this, but I can't get your change-to-lowercase code to do anything. I don't have much experience with dispatcher calls and I couldn't debug it.

Code: Select all

Sub CaseToggle
Doc = ThisComponent
Selections = Doc.CurrentSelection
FirstSelection = Selections.getByIndex(0)
TextString = FirstSelection.String

If UCASE(TextString) = TextString then
	TextString = LCASE(TextString)
Else
	TextString = UCASE(TextString)
End if

FirstSelection.String = TextString
End sub
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
rygle
Posts: 6
Joined: Wed Sep 09, 2009 9:47 am

Re: How to make a sentence uppercase

Post by rygle »

I know it's been a while, because I'm not much of a programmer and just gave up previously, but I finally figured out how to toggle the case of a text selection between upper case and lower case like I wanted to do in my earlier posts. This works on OOo 3.3 and I presume it would work on earlier versions.

Code: Select all

Sub CaseToggle
rem Read the case state of the current selection
If ThisComponent.CurrentSelection.getByIndex(0).CharCaseMap = 1 then
rem If it is upper case, set to lower
   ThisComponent.CurrentSelection.getByIndex(0).CharCaseMap = 0
Else
rem If it is lower case, set to upper
   ThisComponent.CurrentSelection.getByIndex(0).CharCaseMap = 1
End if
rem That's all!
End sub
If you wanted to cycle between all the various case possibilities (see here) then you can try the following;

Code: Select all

Sub CaseCycle
rem Read the case state of the current selection.
rem If it's SmallCaps (the highest possible CaseMap value - 
rem see http://api.openoffice.org/docs/common/ref/com/sun/star/style/CaseMap.html)
rem then set CaseMap=0, which is not necessarily lower case
rem but whatever the underlying text was without any caps effects
If ThisComponent.CurrentSelection.getByIndex(0).CharCaseMap = 4 then
   ThisComponent.CurrentSelection.getByIndex(0).CharCaseMap = 0
Else
rem If it wasn't SmallCaps cycle to the next value
   ThisComponent.CurrentSelection.getByIndex(0).CharCaseMap = ThisComponent.CurrentSelection.getByIndex(0).CharCaseMap + 1
End if
rem That's all!
End sub
Please note that this only works when you have already made a selection.

Hope it helps someone!

Rygle
rygle
Posts: 6
Joined: Wed Sep 09, 2009 9:47 am

Re: How to make a sentence uppercase

Post by rygle »

By the way, I think this will help a lot of the requests above too.

The way I found the right value to change was by using the little glasses icon (Enable Watch) on various variables in the macros above and some I found elsewhere. I had to put in breakpoints in the code and then keep clicking the play/run button to step to the next bit, while watching the values.

Once I had it working, if complicated, I then set about simplifying the code to read and set the value directly without setting any intermediate variables.

I also believe the code at that other link will let you run the code without having a selection. I will fine tune it and post that soon I hope.
Post Reply