I need macro to change the background color of a piece of text in the textbox without changing it in the rest of text. For example, textbox (with default background color) contains text 20 characters long, and characters in positions from 12 to 16 should have background color RGB(255, 20, 20). Is it possible to do this with a macro? Thanks in advance.
Can you do it manually?
It is not possible, because Calc is not a text editor.
What problem are you solving with a spreadsheet? Try using Writer.
However, you can change the color of the substring text.
@JohnSun, @eeigor
Regarding your questions. Quite a large project works on the basis of Calc. So I can’t use Writer. Text fields are a small but necessary part of the project. I have to highlight different pieces of changing text with color many times a day. Didn’t try to do it manually, but in any case the macro works more accurately and much faster.
Macro detects the Textbox according to Name, so there is used textbox1.
Sub ColorTextBox 'change color of some text in Textbox
dim oDoc as object, oPage as object, o as object, i&, j&, oCur as object
oDoc=ThisComponent
if oDoc.DrawPages.hasElements() then
for i=0 to oDoc.DrawPages.Count-1
oPage=oDoc.DrawPages.getByIndex(i)
if oPage.hasElements() then
for j=0 to oPage.Count-1
o=oPage.getByIndex(j)
if o.Name="textbox1" then 'get Textbox by name
oCur=o.createTextCursor
oCur.goToStart(false)
oCur.goRight(12, false)
oCur.goRight(4, true)
oCur.CharColor=RGB(255,20,20)
exit sub
end if
next j
end if
next i
end if
End Sub
Or in ODS run macro ColorTextBox
textbox.ods (9.7 kB)
@KamilLanda
Thank you for your advice, but:
textbox named “txtDet” is located in my dialog “dlgServ”, not on some page of the calc document.
So when I start a macro
dim o as object, oCur as object, dlgServ as object
dlgServ = CreateUnoDialog(DialogLibraries.Standard.dlgServ)
o = dlgServ.GetControl(“txtDet”)
oCur=o.createTextCursor
REM here I get a message “Property or method not found: createTextCursor.”
oCur.goToStart(false)
oCur.goRight(12, false)
oCur.goRight(4, true)
oCur.CharColor=RGB(255,20,20)
What is missing in my definitions?
oCur.CharColor = RGB(255, 20, 20)|
oCur.CharBackColor = RGB(0, 128, 0)|
Use RichTextControl box in dialog: