Restore default tabs in header and footer

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Grytpype
Posts: 11
Joined: Mon Apr 14, 2014 5:26 pm
Location: London

Restore default tabs in header and footer

Post by Grytpype »

I've got a macro (Basic on Windows) that reformats documents. Among other things it adds headers and footers, and puts page numbering (e.g. "page 1 of 6") in the footer. I want the page numbering centred. So I write one tab, chr(9), before "Page", and it moves from the left end to the centre, because there are only three such tab positions in a normal document: left, centre, and right. This worked fine until now.

Now the document I receive has changed format. (This is out of my control.) It's a .docx and it has ten or more tab positions. I don't want any of these, I just want my old left, centre, and right back. (I want all three because I fill them by hand in the header: it's not just about the automatic pagination.) I've read anything I can find about headers, tabs, and styles, without finding an answer. I don't use tabs in the body text, only in the header and footer. Here is the relevant code, cut down to essentials:

Code: Select all

styles = doc.stylefamilies.getbyname("PageStyles")
style = styles("default")
cur = style.footertext.text.createtextcursor
style.footertext.text.insertstring(cur, chr(9) & "page ", 0)
So how do I remove multiple tabs and restore the default three?
LibreOffice 6.2.0.3 on Windows 10 Pro, as of March 2019: just upgraded from 4.1.6.2
User avatar
MTP
Volunteer
Posts: 1620
Joined: Mon Sep 10, 2012 7:31 pm
Location: Midwest USA

Re: Restore default tabs in header and footer

Post by MTP »

Tabs are a property of the "paragraph style" (different than "page styles"). Check out Andrew Pitonyak's book OpenOffice Macros Explained (available for free download on his website), there is a section "Paragraph properties" with a table "Properties supported by the com.sun.style.ParagraphProperties service" including the property "ParaTabStops":
This is an array of structures of type com.sun.star.style.TabStop. Each structure contains the following properties:
•Position – Long integer position relative to the left border.
•Alignment – Alignment of the text range before the tab. This is an enumeration of type com.sun.star.style.TabAlign. Valid values include LEFT, RIGHT, CENTER, DECIMAL, and DEFAULT.
•DecimalChar – Specifies which character is the decimal.
•FillChar – Character used to fill space between the text.
OpenOffice 4.1.1 on Windows 10, HSQLDB 1.8 split database
Grytpype
Posts: 11
Joined: Mon Apr 14, 2014 5:26 pm
Location: London

Re: Restore default tabs in header and footer

Post by Grytpype »

Thank you. With that I've made a crude fix. I don't know whether my problem is 'solved'. I haven't got tabs at left, centre, and right as I wanted, but at left, 90 mm from left, and 160 mm from left, which is good enough for these documents. I didn't work out what any of the other fields in the struct did, but it turns out they're not needed. I just set the position:

Code: Select all

dim tabstops(2) as new com.sun.star.style.TabStop
tabstops(0).position = 0
tabstops(1).position = 9000
tabstops(2).position = 16000
cur.paratabstops = tabstops
LibreOffice 6.2.0.3 on Windows 10 Pro, as of March 2019: just upgraded from 4.1.6.2
Post Reply