1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
| 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 |