I have a writer document with text in it. I am trying to do the following.
If character="[" Bold the item
After the first state triggers keep bolding until "]" occurs, then bold that item and from then on reset to normal text. Continue till document searched.
I am totally confused with the object commands.
Thanks for any help
Sub ChangeFonts()
Dim Flag as boolean
Dim doc as object
dim page as ThisComponent
For i = 1 to ActiveDocument.Range.Characters.Count
if doc.Range.Character(i).Text="[" then Flag=true
if Flag=true then doc.Range.Character(i).Font.Bold=true
if doc.Range.Character(i).Text="]" then Flag=false
Next
End Sub
Last edited by Hagar Delest on Tue Mar 16, 2021 2:51 pm, edited 2 times in total.
Reason:tagged solved.
Sub bracketedToBold
Dim vDescriptor, vFound
vDescriptor = ThisComponent.createSearchDescriptor()
vDescriptor.SearchString = "["
vFound = ThisComponent.findFirst(vDescriptor)
Do While Not IsNull(vFound)
a= vfound.start
vDescriptor.SearchString = "]"
vFound = ThisComponent.findNext( vFound, vDescriptor)
if not isnull(vfound) then
b = vfound.end
tc = vfound.text.createtextcursorbyrange(a)
tc.gotorange(b,true)
'msgbox tc.string
tc.CharWeight = com.sun.star.awt.FontWeight.BOLD
else
exit do
end if
vDescriptor.SearchString = "["
vFound = ThisComponent.findNext( vFound.End, vDescriptor)
Loop
End Sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe,
Thanks very much this is ideal.
I intend on studying your answer as I'd like to understand what is happening.
I was approaching it like vb, not sure how it is different though it obviously is.
Thanks again
'Listing 7.41: Perform a simple search based on words.
Sub SimpleSearchExample
Dim vDescriptor, vFound
' Create a descriptor from a searchable document.
vDescriptor = ThisComponent.createSearchDescriptor()
' Set the text for which to search and other
' http://api.openoffice.org/docs/common/ref/com/sun/star/util/SearchDescriptor.html
With vDescriptor
.SearchString = "hello"
' These all default to false
.SearchWords = true
.SearchCaseSensitive = False
End With
' Find the first one
vFound = ThisComponent.findFirst(vDescriptor)
Do While Not IsNull(vFound)
Print vFound.getString()
vFound.CharWeight = com.sun.star.awt.FontWeight.BOLD
vFound = ThisComponent.findNext( vFound.End, vDescriptor)
Loop
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)