Page 1 of 1

Code to search and replace only till end of document

PostPosted: Sat Jul 04, 2020 5:20 pm
by ptbento
Hello,

I would like to have code to search and replace only till end of document. I have a macro that uses search and replace but does the replacements in all document. What would I have to tweak to achieve that goal?

Code: Select all   Expand viewCollapse view

dim args35(18) as new com.sun.star.beans.PropertyValue
args35(0).Name = "SearchItem.StyleFamily"
args35(0).Value = 2
args35(1).Name = "SearchItem.CellType"
args35(1).Value = 0
args35(2).Name = "SearchItem.RowDirection"
args35(2).Value = true
args35(3).Name = "SearchItem.AllTables"
args35(3).Value = false
args35(4).Name = "SearchItem.Forward"
args35(4).Value = false
args35(5).Name = "SearchItem.Pattern"
args35(5).Value = false
args35(6).Name = "SearchItem.Content"
args35(6).Value = false
args35(7).Name = "SearchItem.AsianOptions"
args35(7).Value = false
args35(8).Name = "SearchItem.AlgorithmType"
args35(8).Value = 0
args35(9).Name = "SearchItem.SearchFlags"
args35(9).Value = 65536
args35(10).Name = "SearchItem.SearchString"
args35(10).Value = "xyzzyx"
args35(11).Name = "SearchItem.ReplaceString"
args35(11).Value = ""
args35(12).Name = "SearchItem.Locale"
args35(12).Value = 255
args35(13).Name = "SearchItem.ChangedChars"
args35(13).Value = 2
args35(14).Name = "SearchItem.DeletedChars"
args35(14).Value = 2
args35(15).Name = "SearchItem.InsertedChars"
args35(15).Value = 2
args35(16).Name = "SearchItem.TransliterateFlags"
args35(16).Value = 1280
args35(17).Name = "SearchItem.Command"
args35(17).Value = 0
args35(18).Name = "Quiet"
args35(18).Value = true

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args35())
Code: Select all   Expand viewCollapse view



Thanks,
Paulo

Re: Code to search and replace only till end of document

PostPosted: Sat Jul 04, 2020 8:21 pm
by John_Ha
Try recording a macro to do what you want.

I recorded this macro which searches for amet from cursor forwards and does not find it if it is before the cursor. You should be able to record your own or hack it. I suspect, but haven't tested, it is the args1() which selects forward as opposed to all.

Code: Select all   Expand viewCollapse view
sub z_end
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(18) 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.Backward"
args1(4).Value = false
args1(5).Name = "SearchItem.Pattern"
args1(5).Value = false
args1(6).Name = "SearchItem.Content"
args1(6).Value = false
args1(7).Name = "SearchItem.AsianOptions"
args1(7).Value = false
args1(8).Name = "SearchItem.AlgorithmType"
args1(8).Value = 0
args1(9).Name = "SearchItem.SearchFlags"
args1(9).Value = 65536
args1(10).Name = "SearchItem.SearchString"
args1(10).Value = "amet"                 
args1(11).Name = "SearchItem.ReplaceString"
args1(11).Value = ""
args1(12).Name = "SearchItem.Locale"
args1(12).Value = 255
args1(13).Name = "SearchItem.ChangedChars"
args1(13).Value = 2
args1(14).Name = "SearchItem.DeletedChars"
args1(14).Value = 2
args1(15).Name = "SearchItem.InsertedChars"
args1(15).Value = 2
args1(16).Name = "SearchItem.TransliterateFlags"
args1(16).Value = 1280
args1(17).Name = "SearchItem.Command"
args1(17).Value = 0
args1(18).Name = "Quiet"
args1(18).Value = true

dispatcher.executeDispatch(document, ".uno:ExecuteSearch", "", 0, args1())

end sub

Re: Code to search and replace only till end of document

PostPosted: Sat Jul 04, 2020 9:26 pm
by Lupp
I understood "till end of document".

However, I missed the document type and information about what to search for and where to start.
Since the code suggests the question is about spreadsheets, I am a bit confused. By default an F&R there should be confined to one sheet. Where to start I cannot get from the code, and I don't know how to interpret the SearchFlags value.

Thus I can only give a hint: It's rather simple to do a Search or a Replace by API elements without relying on the dispatcher. If I get answers to my question, I might be able to suggest a simple and better readable solution.