Libreoffice writer Macro searching document

Hi,

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("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(20) as new com.sun.star.beans.PropertyValue
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

Hi

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("com.sun.star.frame.DispatchHelper")
	dispatcher.executeDispatch(oDoc.CurrentController.Frame, ".uno:PrintPreview", "", 0, Array())
else
	oCursor = oDoc.CurrentController.ViewCursor
	oCursor.gotoRange(oResult, false)
	msgbox "Found", 64, "Title"
end if

end sub

Regards

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?

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.

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!

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!

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!

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

See book Useful Macro Information For OpenOffice.org 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…

See book Useful Macro Information For OpenOffice.org 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…

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.search(opt)
util.selectText(doc, found)

See doc: http://easydev.readthedocs.org/en/latest/liboapp.html#search