[Solved] Modify search and replace by limited to selection?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
sungkhum
Posts: 40
Joined: Sun Mar 29, 2009 7:59 am

[Solved] Modify search and replace by limited to selection?

Post by sungkhum »

Hi modified this macro code from http://user.services.openoffice.org/en/ ... =21&t=2437
But I need to limit the find and replace to the current selection. Can anyone tell me how to do that?

Thanks!

Code: Select all

Sub KhmerNumbers
oDoc = thisComponent
aFind = Array("1", "2","3" ,"4", "5" ,"6" ,"7", "8", "9", "0")
aReplace = Array("១", "២", "៣", "៤", "៥", "៦", "៧", "៨", "៩", "០")
aRayCount = 0
FandR = oDoc.createReplaceDescriptor
FandR.SearchCaseSensitive = true
FandR.SearchRegularExpression = true
While aRayCount <= uBound(aFind)
FandR.setSearchString(aFind(aRayCount))
FandR.setReplaceString(aReplace(aRayCount))
aRayCount = aRayCount + 1
oDoc.ReplaceAll(FandR)
Wend
End Sub
Last edited by sungkhum on Thu Apr 07, 2011 10:59 am, edited 1 time in total.
OOo 3.3 and LibreOffice 3.4.5 on MS Windows 7 64-bit
User avatar
Zizi64
Volunteer
Posts: 11360
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Modify this search and replace to be limited to selectio

Post by Zizi64 »

Hi,

Code: Select all

Sub KhmerNumbers
oDoc = thisComponent
aFind = Array("1", "2","3" ,"4", "5" ,"6" ,"7", "8", "9", "0")

aReplace = Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")   ' just for trying)
'aReplace = Array("១", "២", "៣", "៤", "៥", "៦", "៧", "៨", "៩", "០")

aRayCount = 0

oRange  = oDoc.getCurrentSelection() ' modified by Zizi64
FandR = oRange.createReplaceDescriptor() ' modified by Zizi64

FandR.SearchCaseSensitive = true
FandR.SearchRegularExpression = true
While aRayCount <= uBound(aFind)
FandR.setSearchString(aFind(aRayCount))
FandR.setReplaceString(aReplace(aRayCount))
aRayCount = aRayCount + 1

oRange.ReplaceAll(FandR)  ' modified by Zizi64

Wend
End Sub
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
sungkhum
Posts: 40
Joined: Sun Mar 29, 2009 7:59 am

Re: Modify this search and replace to be limited to selectio

Post by sungkhum »

Thanks Zizi64,

I get an error: createReplaceDescriptor property or method not found when I run the macro in Writer.

Any idea why?

Thanks for your time,
Nathan
OOo 3.3 and LibreOffice 3.4.5 on MS Windows 7 64-bit
User avatar
Zizi64
Volunteer
Posts: 11360
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Modify this search and replace to be limited to selectio

Post by Zizi64 »

Sorry...
I thought: you want to use this macro in the Calc...

I tried it in the Calc, it was worked fine...
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
sungkhum
Posts: 40
Joined: Sun Mar 29, 2009 7:59 am

Re: Modify this search and replace to be limited to selectio

Post by sungkhum »

Does it not work in Writer (the macro is for use in writer, not calc, sorry for not specifying)?

-nathan
OOo 3.3 and LibreOffice 3.4.5 on MS Windows 7 64-bit
User avatar
Zizi64
Volunteer
Posts: 11360
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Modify this search and replace to be limited to selectio

Post by Zizi64 »

Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
sungkhum
Posts: 40
Joined: Sun Mar 29, 2009 7:59 am

Re: Modify this search and replace to be limited to selectio

Post by sungkhum »

Thanks - weird, I wouldn't have thought it would have been such a hard issue.

Thanks again for your time.

-nathan
OOo 3.3 and LibreOffice 3.4.5 on MS Windows 7 64-bit
User avatar
Zizi64
Volunteer
Posts: 11360
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Modify this search and replace to be limited to selectio

Post by Zizi64 »

Maybe it is hard issue only for me. :oops:
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
Posts: 11360
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Modify this search and replace to be limited to selectio

Post by Zizi64 »

This code works fine for me in Writer:
(It is a modified code, based on third code snippet of the linked webpage: http://www.oooforum.org/forum/viewtopic.phtml?t=64090)

Code: Select all

Sub Replace2()
Dim oDoc,oText,oVC,oStart,oEnd,oFind,FandR
oDoc = ThisComponent : oText = oDoc.Text
oVC = oDoc.CurrentController.getViewCursor
    aFind = Array("1", "2","3" ,"4", "5" ,"6" ,"7", "8", "9", "0")
    'aReplace = Array("១", "២", "៣", "៤", "៥", "៦", "៧", "៨", "៩", "០")
    aReplace = Array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")
	aRayCount = 0
	While aRayCount <= uBound(aFind)
		oStart = oText.createTextCursorByRange(oVC.Start)
		If Not oVC.isCollapsed then oEnd = oText.createTextCursorByRange(oVC.End)
			FandR = oDoc.createReplaceDescriptor
			With FandR
 				.SearchString = aFind(aRayCount)
 				.ReplaceString = aReplace(aRayCount)
 				.SearchWords = false
			End With
		If isEmpty(oEnd) then 'Do whole document.
  			oDoc.replaceAll(FandR)
 		Else 'Do selection.
  			Do
   				oFind = oDoc.FindNext(oStart.End,FandR)
   				If isNull(oFind) then Exit Do
   				If oText.compareRegionEnds(oFind,oEnd) < 0 then Exit Do
   				oFind.setString(FandR.ReplaceString)
   				oFind = oDoc.FindNext(oFind.End,FandR)
 	 		Loop
		EndIf
	aRayCount = aRayCount + 1
    Wend		
End Sub
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
sungkhum
Posts: 40
Joined: Sun Mar 29, 2009 7:59 am

Re: Modify this search and replace to be limited to selectio

Post by sungkhum »

Awesome - works perfect! Thanks!
OOo 3.3 and LibreOffice 3.4.5 on MS Windows 7 64-bit
torrua
Posts: 1
Joined: Wed Oct 05, 2022 3:09 pm

Re: [Solved] Modify search and replace by limited to selection?

Post by torrua »

Let me add pythonic interpretation for future searchers:

Code: Select all

import uno
def replace():
    document = uno.getComponentContext().getServiceManager().createInstanceWithContext(
        "com.sun.star.frame.Desktop", uno.getComponentContext()).getCurrentComponent()
    text = document.Text
    view_cursor = document.getCurrentController().getViewCursor()

    array_find = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]
    array_replace = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
    start = text.createTextCursorByRange(view_cursor.Start)
    end = None if view_cursor.isCollapsed() else text.createTextCursorByRange(view_cursor.End)

    for f, r in zip(array_find, array_replace):
        replace_descriptor = document.createReplaceDescriptor()
        replace_descriptor.SearchString = f
        replace_descriptor.ReplaceString = r
        replace_descriptor.SearchWords = False

        if end is None:
            document.replaceAll(replace_descriptor)
            continue

        while True:
            find = document.findNext(start.End, replace_descriptor)
            if not find or text.compareRegionEnds(find, end) < 0:
                break
            find.setString(replace_descriptor.ReplaceString)
OpenOffice 4 on Windows 10
Post Reply