Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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)
  • Selects the found text if this is the case

Macro:

sub FindText

dim oDoc as object
dim oSearch as object
dim oResult as object
dim oCursor 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"
else
    oCursor = oDoc.CurrentController.ViewCursor
    oCursor.gotoRange(oResult, false)
    msgbox "Found", 64, "Title"
end if

end sub

Regards

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