I tried the detection via .getTransferable (that is safer than system clipboard) and it seems OK. .uno:InsertText replaces the selection inside the Textbox.
Sub CHANGE_TextBox 'change selected text in Textbox or Change all Text in Textbox if single Textbox is selected; if Visible Cursor is in Textbox but no text is selected then it shows the Name of Textbox
dim oDoc as object, oSel as object, v as variant
oDoc=ThisComponent
oSel=oDoc.CurrentController.Selection.getByIndex(0) 'only 1st selection
if oSel.supportsService("com.sun.star.drawing.TextShape") then 'selection is TextShape
v=getStringFromCopy(oDoc)
if IsNull(v) then 'SELECTION 1
oSel.String=changeCase(oSel.String) 'change all Text in Textbox
elseif v="" then 'SELECTION 3
msgbox oSel.Name
else 'SELECTION 2
dim args0(0) as new com.sun.star.beans.PropertyValue, oCur as object
args0(0).Name="Text"
args0(0).Value=changeCase(v)
uno(oDoc, "InsertText", args0) 'replace selection by changed text
end if
end if
End Sub
Function changeCase(sChange$)
if UCase(sChange)=sChange then 'If in UPPER CASE
sChange=LCase(sChange) 'change to lower case
elseif LCase(sChange)=sChange then 'If lower case, make Title Case
c1=UCase(Left(sChange, 1)) 'get the first character and convert to upper
Mid(sChange, 1, 1, c1) 'replace that char in sChange
for i=2 to Len(sChange) 'scan the string look for separators
c1=Mid(sChange, i, 1)
if (c1=" " OR c1=Chr(9)) then 'spaces and tabs
c1=UCase(Mid(sChange, i+1, 1)) 'change next character.
Mid(sChange, i+1, 1, c1) '(leave space alone and) put cap back in
endif
next
else
sChange=UCase(sChange) 'Otherwise it's mixed (eg title), change to UPPER
end if
changeCase=sChange
End Function
Function getStringFromCopy(oDoc as object) as variant 'Ctrl+C via .getTransferable() and return the copied text as string
on local error goto bug
dim data as object, aFlavor as new com.sun.star.datatransfer.DataFlavor
data=oDoc.CurrentController.getTransferable()
for each aFlavor in data.TransferDataFlavors
if aFlavor.HumanPresentableName="Text" AND aFlavor.MimeType="text/plain;charset=utf-16" then 'there is Text in copied data
getStringFromCopy=data.getTransferData(aFlavor)
exit function
end if
next
getStringFromCopy=Nothing
exit function
bug:
bug(Erl, Err, Error, "getStringFromCopy")
End Function
Sub bug(sErl$, sErr$, sError$, sFce$) 'show error message and stop macro
msgbox("line: " & sErl & chr(13) & sErr & ": " & sError, 16, sFce)
stop
End Sub
Sub uno(oDoc as object, s$, optional param()) 'Uno command in oDoc
if isMissing(param) then param=array()
s=".uno:" & s
createUnoService("com.sun.star.frame.DispatchHelper").executeDispatch(oDoc.CurrentController.Frame, s, "", 0, param)
End Sub