From The Document Foundation Wiki
< Macros‎ | Writer
Jump to: navigation, search

Template:TopMenu/en Template:Menu/en Template:Menu.Macros/en

Other languages:
English • ‎français • ‎italiano


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[1] interface along with the service[2] to define the properties of the search, including: search text, case sensitivity, use of regular expressions, etc.[3] service is used to display the located text.


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 ',
    Dim oSearchZone As Object '
    Dim oFoundZone As Object '
    Dim oFoundAfterZone As Object '
    Dim oCursVisible As Object '

    oDoc = ThisComponent ' compliant document
    oSearchZone = oDoc.createSearchDescriptor()
    With oSearchZone '
        .SearchString = "start of explanation"
    End With
    oFoundZone = oDoc.findFirst(oSearchZone)

    If Not(IsNull(oFoundZone)) Then
        With oSearchZone '
            .SearchString = "end of explanation"
        End With
        oFoundAfterZone = oDoc.findNext(oFoundZone.End, oSearchZone)
        If Not(IsNull(oFoundAfterZone)) Then
            oCursVisible = oDoc.currentcontroller.viewCursor
            With oCursVisible '
                .gotoRange(oFoundZone.End, false)
                .gotoRange(oFoundAfterZone.Start, True)
                MsgBox .String, MB_ICONINFORMATION, "Text was found!"
            End With
            MsgBox "end tag was NOT found", MB_ICONINFORMATION, "Text search"
        End If
        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


def SearchText():
    """Search 1 Highlight text found between two tags
    oDoc = XSCRIPTCONTEXT.getDocument()  # 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")
            MsgBox('end tag was NOT found',MB_ICONINFORMATION, "Text search")
        MsgBox("start tag was NOT found",MB_ICONINFORMATION, 'Text search')

def MsgBox(prompt="", buttons=MB_ICONINFORMATION, title=""):  # Text search

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

ODT file to test macro