Scroll to Writer Page

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: 2777
Joined: Wed Mar 09, 2016 2:40 pm

Scroll to Writer Page

Post by JeJe »

Scroll to Writer page

Code: Select all

	'Scroll to writer page
	'note:ASSUMES ALL PAGES ARE OF THE SAME HEIGHT
	'this works on my machine in OO may not be generally true
'--------explanation---------
	'the accessible context allows access to the window scrollbar
	'in OO the scrollbar position is in half points
	'the appropriage scroll position can be found by creating a document with a set number of pages of the required height
	'and getting the maximum scroll position by calling getVScrollvalue(doc, 1)
	'by doing that I got the below constants for A4 and Letter pages
	'there's an initial value plus a value for the height of each page
	'the document itself is measured in fractions of a mm
	'converting from that value to the half point value gives slight varience
	'without knowing the formula used the only way to do this seems to be by using these constants

sub Main
	'	gotopage thiscomponent,2
	gotoPageWithViewCursor thiscomponent,1

end sub


sub gotopage(doc,pagei) 'go to chosen page

	pname = doc.currentcontroller.viewcursor.PageStyleName
	if pagei < 1 then pagei =1
	pagecount =doc.currentcontroller.pagecount
	if pagei > pagecount +1 then  pagei = pagecount +1
	ht = doc.stylefamilies.getbyname("PageStyles").getbyname(pname).height
	select case ht
	case 29700 'A4
		fbit = 852 'first value added to all page sizes
		pincrement = 17122 'page size
	case 27940 'Letter
		fbit = 852
		pincrement = 16124
	end select
	if pagei = 1 then
		svalue =0
	else
		svalue = fbit +(( pagei-1) * pincrement) 'first value + previous pages * page size
	end if
	setVScrollvalue(doc,svalue)
end sub


function setVScrollvalue(doc,svalue) 'set scrollbar value which sets the scroll position
	dim found as boolean
	found = false
	comp =doc.currentcontroller.frame.getcomponentwindow
	compchild=comp.getAccessibleContext.getAccessibleChild(0)
	'	on error resume next
	for j = 0 to compchild.getAccessibleContext.getAccessibleChildcount -1
		with compchild.getAccessibleContext.getAccessibleChild(j)
			if .getAccessibleContext.getAccessibleName = "Vertical scroll bar" then found = true
			if found then
				.getAccessibleContext.setcurrentvalue ( svalue)
				exit for
			end if
		end with
	next
end function



function getVScrollvalue(doc, vtype) 'gets the scrollbar position or scrollbar maximum value
	dim found as boolean
	found = false
	comp =doc.currentcontroller.frame.getcomponentwindow
	compchild=comp.getAccessibleContext.getAccessibleChild(0)
	'	on error resume next
	for j = 0 to compchild.getAccessibleContext.getAccessibleChildcount -1
		with compchild.getAccessibleContext.getAccessibleChild(j)
			if .getAccessibleContext.getAccessibleName = "Vertical scroll bar" then
				select case vtype
				case 0
					getVScrollvalue= .getAccessibleContext.currentvalue
				case 1
					getVScrollvalue= .getAccessibleContext.maximumvalue
				case 2
					getVScrollvalue= .getAccessibleContext.minimumvalue
				end select
				exit for
			end if
		end with
	next
end function

sub gotoPageWithViewCursor(doc, pageno)
	vc = doc.currentcontroller.viewcursor
	VC.jumpToPage(pageno,false)
	gotopage doc,pageno
end sub


Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply