[Solved] Running macro on Writer

Creating a macro - Writing a Script - Using the API

[Solved] Running macro on Writer

Postby valter » Tue Mar 16, 2021 7:21 am

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
Last edited by Hagar Delest on Tue Mar 16, 2021 2:51 pm, edited 2 times in total.
Reason: tagged solved.
OpenOffice 4.1.9 on Windows 10
valter
 
Posts: 2
Joined: Tue Mar 16, 2021 4:57 am

Re: Running macro on Writer

Postby JeJe » Tue Mar 16, 2021 11:01 am

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
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1705
Joined: Wed Mar 09, 2016 2:40 pm

Re: Running macro on Writer

Postby JeJe » Tue Mar 16, 2021 11:53 am

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
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1705
Joined: Wed Mar 09, 2016 2:40 pm

Re: Running macro on Writer

Postby valter » Tue Mar 16, 2021 2:31 pm

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
OpenOffice 4.1.9 on Windows 10
valter
 
Posts: 2
Joined: Tue Mar 16, 2021 4:57 am

Re: Running macro on Writer

Postby JeJe » Tue Mar 16, 2021 2:57 pm

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
Openoffice 4.1.6
Windows 8
JeJe
Volunteer
 
Posts: 1705
Joined: Wed Mar 09, 2016 2:40 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 13 guests