Multiple Selections in Writer by macro

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Multiple Selections in Writer by macro

Post 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



Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Multiple Selections in Writer by macro

Post 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
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply