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 posn0 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