Page 1 of 1

[Solved] Running macro on Writer

PostPosted: Tue Mar 16, 2021 7:21 am
by valter
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

Code: Select all   Expand viewCollapse view
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

Re: Running macro on Writer

PostPosted: Tue Mar 16, 2021 11:01 am
by JeJe
You're thinking this is like VBA in Microsoft Word - its different.

Is your bracketed text all in the range of a single paragraph?

If so you can do a find with regex enabled... either by macro or using the find&replace dialog... with this expression:

\[.+?\]

Adapted from

https://community.notepad-plus-plus.org ... entheses/2

Re: Running macro on Writer

PostPosted: Tue Mar 16, 2021 11:53 am
by JeJe
If multiple paragraphs are spanned try this:

Code: Select all   Expand viewCollapse view
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

Re: Running macro on Writer

PostPosted: Tue Mar 16, 2021 2:31 pm
by valter
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

Re: Running macro on Writer

PostPosted: Tue Mar 16, 2021 2:57 pm
by JeJe
Its a quick modification of a listing in "Useful Macro Information For OpenOffice" By Andrew Pitonyak

https://www.pitonyak.org/oo.php

His books and the links there are about the best introduction to OOBasic programming

His original, slightly simpler sub is

Code: Select all   Expand viewCollapse view
'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