Macro for identifying text between two boundary strings

    From The Document Foundation Wiki
    < Macros‎ | Writer


    Other languages:

    Description

    This Basic macro for Writer identifies text contained between two boundary strings (i.e. a "start tag" and an "end tag"). For example, the text which is found between the beginning and end of an explanation.

    The principle is to search for a unique "start tag" which gives a starting position to search for the "end tag". These two positions are selected via the visible cursor from the end of the "start tag" to the beginning of the "end tag".

    The search is performed using the createSearchDescriptor method of the com.sun.star.util.XSearchable[1] interface along with the com.sun.star.util.SearchDescriptor service[2] to define the properties of the search, including: search text, case sensitivity, use of regular expressions, etc.

    com.sun.star.text.XTextViewCursor[3] service is used to display the located text.

    Code

    In the below code, the start & end tags are "start of explanation" and "end of explanation", respectively.

    Option Explicit
    
    Sub SearchText()
        '''Search & Highlight text found between two tags'''
    
        Dim oDoc As Object ' com.sun.star.text.GenericTextDocument, com.sun.star.util.XSearchable
        Dim oSearchZone As Object ' com.sun.star.util.SearchDescriptor
        Dim oFoundZone As Object ' com.sun.star.uno.XInterface
        Dim oFoundAfterZone As Object ' com.sun.star.uno.XInterface
        Dim oCursVisible As Object ' com.sun.star.text.XTextViewCursor
    
        oDoc = ThisComponent ' com.sun.star.util.XSearchable compliant document
        oSearchZone = oDoc.createSearchDescriptor()
        With oSearchZone ' com.sun.star.util.SearchDescriptor
            .SearchString = "start of explanation"
        End With
        oFoundZone = oDoc.findFirst(oSearchZone)
    
        If Not(IsNull(oFoundZone)) Then
            With oSearchZone ' com.sun.star.util.SearchDescriptor
                .SearchString = "end of explanation"
            End With
            oFoundAfterZone = oDoc.findNext(oFoundZone.End, oSearchZone)
            If Not(IsNull(oFoundAfterZone)) Then
                oCursVisible = oDoc.currentcontroller.viewCursor
                With oCursVisible ' com.sun.star.text.text.XTextViewCursor
                    .gotoRange(oFoundZone.End, false)
                    .gotoRange(oFoundAfterZone.Start, True)
                    MsgBox .String, MB_ICONINFORMATION, "Text was found!"
                End With
            Else
                MsgBox "end tag was NOT found", MB_ICONINFORMATION, "Text search"
            End If
        Else
            MsgBox "start tag was NOT found", MB_ICONINFORMATION, "Text search"
        End If
    
    End Sub ' SearchText

    With Pythonː

    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    
    MB_ICONINFORMATION = 64
    
    def SearchText():
        """Search 1 Highlight text found between two tags
        
        oSearchZone         com.sun.star.util.SearchDescriptor
        oFound(After)Zone   com.sun.star.uno.XInterface
        oCursVisible        com.sun.star.text.XTextViewCursor
        """
        oDoc = XSCRIPTCONTEXT.getDocument()  # com.sun.star.util.XSearchable compliant document
        oSearchZone = oDoc.createSearchDescriptor()
        oSearchZone.SearchString = "start of explanation"
        oFoundZone = oDoc.findFirst(oSearchZone)
        if oFoundZone:
            oSearchZone.SearchString = 'end of explanation'
            oFoundAfterZone = oDoc.findNext(oFoundZone.End, oSearchZone)
            if oFoundAfterZone:
                oCursVisible = oDoc.CurrentController.ViewCursor
                oCursVisible.gotoRange(oFoundZone.End, False)
                oCursVisible.gotoRange(oFoundAfterZone.Start, True)
                MsgBox(oCursVisible.String,MB_ICONINFORMATION, "Text was found")
            else:
                MsgBox('end tag was NOT found',MB_ICONINFORMATION, "Text search")
        else:
            MsgBox("start tag was NOT found",MB_ICONINFORMATION, 'Text search')
    
    def MsgBox(prompt="", buttons=MB_ICONINFORMATION, title=""):  # Text search
        pass

    Python standard output file is not available when running Python macros. Input/Output to Screen help page exposes alternatives.

    ODT file to test macro

    Notes