Page 1 of 1

Multiple Selections in Writer by macro

Posted: Sat Aug 19, 2023 2:30 am
by JeJe
We can do it with dispatch commands. Example below should create two separate selections in a document with some text that begins in the normal selection mode.

(Another way is to mark the text in some way such as with charflash and then do a find for charflash - unfortunately that produces a selection comprised of the text of each paragraph, not the full text including paragraph end marks)

Code: Select all

sub test

godown 2,true 'select 2 lines

for i = 0 to 1 'turn on selection mode ASSUMES WE BEGAN WITH NORMAL (STD) selection mode 
selectionMode
next

godown 2,false 'go down 2 linew without selecting

goright 10,true 'go right 10 characters selecting
end sub

sub selectionMode
dim document   as object
dim dispatcher as object
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dispatcher.executeDispatch(document, ".uno:SelectionMode", "", 0, Array())
end sub


sub GoDown(n, doselect)
dim document   as object
dim dispatcher as object
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dim args1(1) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Count"
args1(0).Value = n
args1(1).Name = "Select"
args1(1).Value = doselect
dispatcher.executeDispatch(document, ".uno:GoDown", "", 0, args1())
end sub


sub goRight(n,doselect)
dim document   as object
dim dispatcher as object
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
dim args1(1) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Count"
args1(0).Value = n
args1(1).Name = "Select"
args1(1).Value = doselect
dispatcher.executeDispatch(document, ".uno:GoRight", "", 0, args1())
end sub




Re: Multiple Selections in Writer by macro

Posted: Sat Aug 19, 2023 3:19 am
by JeJe
This SetSelectionMode sub should set the selection mode to ADD regardless of the current mode using the text on the status bar.

(Works for me in OO in English, not tested elsewhere)

Code: Select all


sub test

thiscomponent.currentcontroller.select(thiscomponent.text.start)

SetSelectionMode "ADD"

godown 2,true 'select 2 lines
godown 2,false 'go down 2 linew without selecting
goright 10,true 'go right 10 characters selecting

goright 10,false 
goright 10,true

end sub



Sub SetSelectionMode(SelectionModeName) '"ADD" "STD" "BLK" "EXT"
	els =thiscomponent.currentcontroller.frame.layoutmanager.elements
	for i = 0 to ubound(els)
		if instr(1,els(i).resourceurl,"statusbar") then
			with els(i).realinterface.accessiblecontext
				for j= 0 to .getaccessiblechildCount
					if .getaccessiblechild(j).AccessibleName = "Selection Mode" then
'						if .getaccessiblechild(j).text <> SelectionModeName then
						for k = 1 to 4
						selectionMode
						if .getaccessiblechild(j).text = SelectionModeName then exit for
						next
'						end if
						exit for
					end if
				next
			end with
		end if
	next
End Sub