[Issue] Uppercase to lowercase with capital letter
Posted: Fri Jun 27, 2008 4:28 pm
Edit: Hagar: split the thread from: [Solved] Format uppercase to lowercase with capital letter related to Calc. |
User community support forum for Apache OpenOffice, LibreOffice and all the OpenOffice.org derivatives
https://forum.openoffice.org/en/forum/
Edit: Hagar: split the thread from: [Solved] Format uppercase to lowercase with capital letter related to Calc. |
Code: Select all
Sub CaseToggle
' CaseToggleMk3 phb 20070828
' Should preserve formatting and bookmarks in Writer
' Should work on text within a table in Writer (but not on whole cells)
' Should work on multiple words
' Does not handle non-contiguous text selection
dim document,dispatcher,vcursor,oText,alpha,omega,ThisSeln,oBookmark As Object
dim seln,nextword,nextbit as string
dim loops,posn,nSelCount as integer
dim ctogbmpv(0) as new com.sun.star.beans.PropertyValue
On Error GoTo ExitPoint ' In case selection is not text
nSelCount = thiscomponent.getCurrentSelection().getCount()
if nSelCount>1 then
msgbox ("There seems to be more than one piece of text selected." & _
chr(10) & "Try making one selection.",0,"Case Conversion")
goto ExitPoint
endif
loops=0
document=ThisComponent.CurrentController.Frame
dispatcher=createUnoService("com.sun.star.frame.DispatchHelper")
vcursor=ThisComponent.currentcontroller.getViewCursor()
ctogbmpv(0).Name="Bookmark"
ctogbmpv(0).Value="ctogbm"
dispatcher.executeDispatch(document, ".uno:DeleteBookmark", "", 0, ctogbmpv())
dispatcher.executeDispatch(document, ".uno:InsertBookmark", "", 0, ctogbmpv())
GetSeln: ' Much here is taken from Andrew Brown and Andrew Pitonyak
oText=vcursor.getText()
alpha=vcursor.getStart()
omega=vcursor.getEnd()
ThisSeln=oText.createTextCursorByRange(alpha)
ThisSeln.goToRange(omega,TRUE)
seln=ThisSeln.getString()
if seln<>"" then ' There is a selection - go and change case
goto ChgCase
endif ' make a selection to change
dispatcher.executeDispatch(document, ".uno:GoToNextWord", "", 0, Array())
dispatcher.executeDispatch(document, ".uno:WordLeftSel", "", 0, Array())
loops=loops+1
if loops=2 then goto FinishOff ' in case we failed to find any text to select
goto GetSeln
ChgCase: 'Check case of seln and alter accordingly
if seln=ucase(seln) then 'seln is already UPPER - set to lower
dispatcher.executeDispatch(document, ".uno:ChangeCaseToLower", "", 0, Array())
goto SetToTitle
endif
if seln=lcase(seln) then 'seln is already lower - set to Title
goto SetToTitle
endif 'seln is probably mixed case - set to UPPER
dispatcher.executeDispatch(document, ".uno:ChangeCaseToLower", "", 0, Array())
goto SetToTitle
SetToTitle:
vcursor.collapsetoStart()
posn=0
do while posn<len(seln)
loops=0
nextbit="1"
do while nextbit=lcase(nextbit) and loops<2
vcursor.goRight(1,TRUE)
dispatcher.executeDispatch(document, ".uno:ChangeCaseToUpper", "", 0, Array())
alpha=vcursor.getStart()
omega=vcursor.getEnd()
ThisSeln=oText.createTextCursorByRange(alpha)
ThisSeln.goToRange(omega,TRUE)
nextbit=ThisSeln.getString()
loops=loops+1
loop
if instr(nextbit," ")>0 or instr(nextbit,chr(10))>0 then
vcursor.goLeft(1,TRUE) ' To avoid problems due to punctuation marks
endif
dispatcher.executeDispatch(document, ".uno:WordRightSel", "", 0, Array())
alpha=vcursor.getStart()
omega=vcursor.getEnd()
ThisSeln=oText.createTextCursorByRange(alpha)
ThisSeln.goToRange(omega,TRUE)
nextword=ThisSeln.getString()
posn=posn + len(nextword)
vcursor.collapsetoEnd()
loop
FinishOff: 'set vcursor back to the original position
oBookmark=ThisComponent.getBookmarks().getByName("ctogbm")
ThisComponent.getCurrentController().select(oBookmark)
dispatcher.executeDispatch(document, ".uno:DeleteBookmark", "", 0, ctogbmpv())
ExitPoint:
end Sub
Code: Select all
sub CaseCorrect
rem ---Starting at cursor, it sets a Capital, then the rest of the word lower. -
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 args4(1) as new com.sun.star.beans.PropertyValue
args4(0).Name = "Count"
args4(0).Value = 1
args4(1).Name = "Select"
args4(1).Value = true
dispatcher.executeDispatch(document, ".uno:GoRight", "", 0, args4())
dispatcher.executeDispatch(document, ".uno:ChangeCaseToUpper", "", 0, Array())
dispatcher.executeDispatch(document, ".uno:Escape", "", 0, Array())
dispatcher.executeDispatch(document, ".uno:WordRightSel", "", 0, Array())
dispatcher.executeDispatch(document, ".uno:ChangeCaseToLower", "", 0, Array())
dispatcher.executeDispatch(document, ".uno:Escape", "", 0, Array())
end sub
I don't see them any more in release candidate 5. What's happened?RGB wrote:3.3 will introduce more options:
Sentence case
lowercase
UPPERCASE
Capitalize Every Word
tOGGLE cASE
Code: Select all
REM ***** BASIC *****
Sub CaseToggle
' CaseToggleMk3 phb 20070828
' Should preserve formatting and bookmarks in Writer
' Should work on text within a table in Writer (but not on whole cells)
' Should work on multiple words
' Does not handle non-contiguous text selection
dim document,dispatcher,vcursor,oText,alpha,omega,ThisSeln,oBookmark As Object
dim seln,nextword,nextbit as string
dim loops,posn,nSelCount as integer
dim ctogbmpv(0) as new com.sun.star.beans.PropertyValue
On Error GoTo ExitPoint ' In case selection is not text
nSelCount = thiscomponent.getCurrentSelection().getCount()
if nSelCount>1 then
msgbox ("There seems to be more than one piece of text selected." & _
chr(10) & "Try making one selection.",0,"Case Conversion")
goto ExitPoint
endif
loops=0
document=ThisComponent.CurrentController.Frame
dispatcher=createUnoService("com.sun.star.frame.DispatchHelper")
vcursor=ThisComponent.currentcontroller.getViewCursor()
ctogbmpv(0).Name="Bookmark"
ctogbmpv(0).Value="ctogbm"
dispatcher.executeDispatch(document, ".uno:DeleteBookmark", "", 0, ctogbmpv())
dispatcher.executeDispatch(document, ".uno:InsertBookmark", "", 0, ctogbmpv())
GetSeln: ' Much here is taken from Andrew Brown and Andrew Pitonyak
oText=vcursor.getText()
alpha=vcursor.getStart()
omega=vcursor.getEnd()
ThisSeln=oText.createTextCursorByRange(alpha)
ThisSeln.goToRange(omega,TRUE)
seln=ThisSeln.getString()
if seln<>"" then ' There is a selection - go and change case
goto ChgCase
endif ' make a selection to change
dispatcher.executeDispatch(document, ".uno:GoToNextWord", "", 0, Array())
dispatcher.executeDispatch(document, ".uno:WordLeftSel", "", 0, Array())
loops=loops+1
if loops=2 then goto FinishOff ' in case we failed to find any text to select
goto GetSeln
ChgCase: 'Check case of seln and alter accordingly
if seln=ucase(seln) then 'seln is already UPPER - set to lower
dispatcher.executeDispatch(document, ".uno:ChangeCaseToLower", "", 0, Array())
goto FinishOff
endif
if seln=lcase(seln) then 'seln is already lower - set to Title
goto SetToTitle
endif 'seln is probably mixed case - set to UPPER
dispatcher.executeDispatch(document, ".uno:ChangeCaseToUpper", "", 0, Array())
goto FinishOff
SetToTitle:
vcursor.collapsetoStart()
posn=0
do while posn<len(seln)
loops=0
nextbit="1"
do while nextbit=lcase(nextbit) and loops<2
vcursor.goRight(1,TRUE)
dispatcher.executeDispatch(document, ".uno:ChangeCaseToUpper", "", 0, Array())
alpha=vcursor.getStart()
omega=vcursor.getEnd()
ThisSeln=oText.createTextCursorByRange(alpha)
ThisSeln.goToRange(omega,TRUE)
nextbit=ThisSeln.getString()
loops=loops+1
loop
if instr(nextbit," ")>0 or instr(nextbit,chr(10))>0 then
vcursor.goLeft(1,TRUE) ' To avoid problems due to punctuation marks
endif
dispatcher.executeDispatch(document, ".uno:WordRightSel", "", 0, Array())
alpha=vcursor.getStart()
omega=vcursor.getEnd()
ThisSeln=oText.createTextCursorByRange(alpha)
ThisSeln.goToRange(omega,TRUE)
nextword=ThisSeln.getString()
posn=posn + len(nextword)
vcursor.collapsetoEnd()
loop
FinishOff: 'set vcursor back to the original position
oBookmark=ThisComponent.getBookmarks().getByName("ctogbm")
ThisComponent.getCurrentController().select(oBookmark)
dispatcher.executeDispatch(document, ".uno:DeleteBookmark", "", 0, ctogbmpv())
ExitPoint:
end Sub