Ask Your Question

Libreoffice writer Macro searching document

asked 2016-02-08 08:31:32 +0100

Thibault gravatar image


I'm trying to create a macro that searches my document for "xx" and if it's found one or more times it should show a messagebox.

Using the script recorder I recorded this but don't know how to continue...:

   REM  *****  BASIC  *****

sub Main
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("")

rem ----------------------------------------------------------------------
dim args1(20) as new
args1(0).Name = "SearchItem.StyleFamily"
args1(0).Value = 2
args1(1).Name = "SearchItem.CellType"
args1(1).Value = 0
args1(2).Name = "SearchItem.RowDirection"
args1(2).Value = true
args1(3).Name = "SearchItem.AllTables"
args1(3).Value = false
args1(4).Name = "SearchItem.SearchFiltered"
args1(4).Value = false
args1(5).Name = "SearchItem.Backward"
args1(5).Value = false
args1(6).Name = "SearchItem.Pattern"
args1(6).Value = false
args1(7).Name = "SearchItem.Content"
args1(7).Value = false
args1(8).Name = "SearchItem.AsianOptions"
args1(8).Value = false
args1(9).Name = "SearchItem.AlgorithmType"
args1(9).Value = 0
args1(10).Name = "SearchItem.SearchFlags"
args1(10).Value = 0
args1(11).Name = "SearchItem.SearchString"
args1(11).Value = "xx"
args1(12).Name = "SearchItem.ReplaceString"
args1(12).Value = ""
args1(13).Name = "SearchItem.Locale"
args1(13).Value = 255
args1(14).Name = "SearchItem.ChangedChars"
args1(14).Value = 2
args1(15).Name = "SearchItem.DeletedChars"
args1(15).Value = 2
args1(16).Name = "SearchItem.InsertedChars"
args1(16).Value = 2
args1(17).Name = "SearchItem.TransliterateFlags"
args1(17).Value = 256
args1(18).Name = "SearchItem.Command"
args1(18).Value = 0
args1(19).Name = "SearchItem.SearchFormatted"
args1(19).Value = false
args1(20).Name = "Quiet"
args1(20).Value = true

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args1())

end sub
edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted

answered 2016-02-09 09:47:23 +0100

pierre-yves samyn gravatar image

updated 2016-02-10 10:23:42 +0100


The macro recorder is limited. On the other hand, programming using the API can not be improvised. Information and resources for finding, creating, using and publishing macros on the wiki.

The following macro:

  • Define the search criteria
  • Finds the first occurrence of the text
  • Displays a message Found/Not Found (documentation here)
  • Open the print preview if not found
  • Selects the found text if this is the case

[UPDATED] Macro:

sub FindText

dim oDoc as object
dim oSearch as object
dim oResult as object
dim oCursor as object
dim dispatcher as object

oDoc = thiscomponent
oSearch = oDoc.createSearchDescriptor()

with oSearch
   .SearchString = "xx"
   .SearchRegularExpression = false
   .SearchCaseSensitive = true
end with

oResult = oDoc.findFirst(oSearch)

if isnull(oResult) then
    msgbox "Not Found", 64, "Title"
    dispatcher = createUnoService("")
    dispatcher.executeDispatch(oDoc.CurrentController.Frame, ".uno:PrintPreview", "", 0, Array())
    oCursor = oDoc.CurrentController.ViewCursor
    oCursor.gotoRange(oResult, false)
    msgbox "Found", 64, "Title"
end if

end sub


edit flag offensive delete link more


This is perfect! I'm trying to also make a print preview show up when it didn't find anything. I found "uno.PrintPreview" but I'm not sure how to use it. Could you maybe change the script so it shows a print preview when it didn't find 'xx'. Thanks btw for the previous script, can I donate to you or something?

Thibault gravatar imageThibault ( 2016-02-09 15:39:31 +0100 )edit

I edited my answer to include print preview. I thank you for your generous offer, but if it suits you, I suggest you to support our worldwide community here.

pierre-yves samyn gravatar imagepierre-yves samyn ( 2016-02-10 10:27:47 +0100 )edit

I already have XD. Thank you very much, this is exactly the macro I was looking for. Thank you again for making my switch to Libreoffice easier!

Thibault gravatar imageThibault ( 2016-02-10 16:18:53 +0100 )edit

Btw, is there actually a way to use a BASIC Macro to click the button 'Synchronise Labels' when creating labels in Writer? Because if there is that's what it should do before showing the print preview. I didn't think there was a way to do that so I didn't think about asking.

Thanks for the updated script Again!

Thibault gravatar imageThibault ( 2016-02-10 17:49:07 +0100 )edit

When running ThisComponent.createSearchDescriptor in LibreCalc, I get a BASIC runtime error that the property or method was not found.

dandv gravatar imagedandv ( 2020-01-14 23:42:10 +0100 )edit

answered 2016-02-09 10:10:01 +0100

JohnSUN gravatar image

See book Useful Macro Information For By Andrew Pitonyak - subroutine SimpleSearchExample (Listing 7.41: Perform a simple search based on words in chapter 7.14. Search And Replace)... and others examples in this chapter... and in this book...

edit flag offensive delete link more

answered 2016-02-09 15:06:21 +0100

mauricio gravatar image

With my estension EasyDev, you can:

util = createUnoService("org.universolibre.EasyDev")
opt = createUnoStruct("org.universolibre.EasyDev.SearchReplace")

doc = ThisComponent
opt.Doc = doc
opt.Search = "test"

found =
util.selectText(doc, found)

See doc:

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-02-08 08:31:32 +0100

Seen: 1,575 times

Last updated: Feb 10 '16