[Solved] [Calc] HEADERTEXT gives an error

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
abhaysobti
Posts: 17
Joined: Tue Jun 01, 2010 10:10 am

[Solved] [Calc] HEADERTEXT gives an error

Post by abhaysobti »

Hi everyone,

When I run the following Macro in sCalc, the lines in bold give an error :

Basic runtime error
Property or Method not found.

Code: Select all

Sub AddHeader(optional doc)
	oDoc = IIf(IsMissing(doc), ThisComponent, doc)
	' turn on headers for default page style
	oStyle = oDoc.StyleFamilies.getByName("PageStyles").getByName("Default")
	oStyle.HeaderIsOn = True
	oStyle.HeaderIsShared = True
	oStyle.HeaderHeight = 500 ' 0.5 cm - header height
	oStyle.HeaderBodyDistance = 0 ' 0 cm - distance from page text area to header
	' decrease the top margin by 0.5 cm (the header's height)
	oPageStyle = oDoc.StyleFamilies.getByName("PageStyles").getByName("Default")
	oPageStyle.TopMargin = oPageStyle.TopMargin - 500
	' Add some regular text. In OpenOffice.org 1.1, newly created headers have a centered
	' tabstop at the center of header area and a right tab stop at the right margin.
	' In OO 1.0, one would have to set tabstops in the macro,
	' otherwise default tab stops of 1,25 cm would be used. Tab is Chr(09).
	[b]oStyle.HeaderText.SetString("MyCompany header text - left" & Chr(09) _
		& "centered text")[/b]	' add some fields
	oCursor = oStyle.HeaderText.createTextCursor()
	oCursor.GotoEnd(false)
	oStyle.HeaderText.insertString(oCursor, Chr(09) & "Page ", false)
	' insert "page number" field
	oField = oDoc.createInstance("com.sun.star.text.TextField.PageNumber")
	oField.NumberingType = 4 ' magic constant: 4=Arabic numbers
	oField.SubType = 1 ' another magic constant (use current page number)
	oStyle.HeaderText.insertTextContent(oCursor, oField, False)
	' more regular text
	oStyle.HeaderText.insertString(oCursor, " of ", false)
	' insert "total pages" field
	oField = oDoc.createInstance("com.sun.star.text.TextField.PageCount")
	oField.NumberingType = 4 ' as above
	oStyle.HeaderText.insertTextContent(oCursor, oField, False)
	' Now set some formatting (reuse cursor object)
	oCursor.gotoStart(false)
	oCursor.gotoEnd(true)
	oCursor.CharHeight = "6" ' font size
	oCursor.CharFontName = "Times New Roman" ' font name
	' set text locale to "no locale" (empty locale object) in order to turn off
	' any spell checking in the header
	Dim aLocale As New com.sun.star.lang.Locale
	' Uncomment the lines below to use a specific locale, e.g. US English, instead
	' aLocale.Language = "en"
	' aLocale.Country = "US"
	oCursor.CharLocale = aLocale
End Sub
Any help will be highly appreciated.

Abhay
Last edited by Hagar Delest on Tue Jun 01, 2010 9:28 pm, edited 1 time in total.
Reason: tagged [Solved].
OpenOffice 3.0 on XP
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: sCalc HEADERTEXT gives an error

Post by RoryOF »

I think SetString should have a small "s" at start viz: setString.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
abhaysobti
Posts: 17
Joined: Tue Jun 01, 2010 10:10 am

Re: sCalc HEADERTEXT gives an error

Post by abhaysobti »

Thanks

I managed to solve the problem.

The fact is, Headertext applies to only swriter documents.

Thanks for the help.

Abhay
OpenOffice 3.0 on XP
Rushna
Posts: 19
Joined: Tue May 09, 2017 12:42 pm

Re: [Solved] [Calc] HEADERTEXT gives an error

Post by Rushna »

Hi..,
I have inserted Header Text successfully.
will you please tell me how to set Header Text in center.
OpenOffice 4.3.1 on Windows 7
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Solved] [Calc] HEADERTEXT gives an error

Post by RoryOF »

Position cursor before it, and press Tab key. You may need to adjust the Style to remove the checkmark for "Automatic" on the Indents and Spacing tab of the style definitions.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Rushna
Posts: 19
Joined: Tue May 09, 2017 12:42 pm

Re: [Solved] [Calc] HEADERTEXT gives an error

Post by Rushna »

I am using properties.
not manually.
how to set style properties for left/Right/Center Header/Footer.
OpenOffice 4.3.1 on Windows 7
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Solved] [Calc] HEADERTEXT gives an error

Post by RoryOF »

Headers and Footers come with predefined tab stops, Left, Centre and Right. Use them.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Rushna
Posts: 19
Joined: Tue May 09, 2017 12:42 pm

Re: [Solved] [Calc] HEADERTEXT gives an error

Post by Rushna »

Hi..,
I have written this code to add header.please write some code to Right/Left/Center Header.


public void HeaderFooter(String s1) throws NoSuchElementException, WrappedTargetException, UnknownPropertyException, PropertyVetoException, com.sun.star.lang.IllegalArgumentException
{
XTextDocument xTextDocument = (XTextDocument) UnoRuntime.queryInterface(
XTextDocument.class, m_xFrame.getController().getModel());
XStyleFamiliesSupplier xSupplier = (XStyleFamiliesSupplier)
UnoRuntime.queryInterface(
XStyleFamiliesSupplier.class, xTextDocument);
XNameAccess xFamilies = (XNameAccess) UnoRuntime.queryInterface (
XNameAccess.class, xSupplier.getStyleFamilies());
XNameContainer xFamily = (XNameContainer) UnoRuntime.queryInterface(
XNameContainer.class, xFamilies.getByName("PageStyles"));
// The style name may be "Default Style" or just "Default"
XStyle xStyle = (XStyle) UnoRuntime.queryInterface(
XStyle.class, xFamily.getByName("Standard"));

XPropertySet xStyleProps = (XPropertySet) UnoRuntime.queryInterface(
XPropertySet.class, xStyle);

xStyleProps.setPropertyValue ("HeaderIsOn", Boolean.TRUE);
xStyleProps.setPropertyValue("HeaderBackColor", Long.BYTES);
xStyleProps.setPropertyValue("HeaderBodyDistance",230);

XText HeaderText = (XText) UnoRuntime.queryInterface(XText.class, xStyleProps.getPropertyValue("HeaderTextRight"));

com.sun.star.text.XTextCursor
xTextCursor = HeaderText.createTextCursorByRange(HeaderText);

HeaderText.insertString(xTextCursor, s1, true);
}
OpenOffice 4.3.1 on Windows 7
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Solved] [Calc] HEADERTEXT gives an error

Post by RoryOF »

Either use a predefined template (which you should set up), or add a centre and a right tab stop to your Header style.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
Rushna
Posts: 19
Joined: Tue May 09, 2017 12:42 pm

Re: [Solved] [Calc] HEADERTEXT gives an error

Post by Rushna »

Hi..,
I am using this code but i am getting error in HeaderText.insertString(xTextCursor, s1, true); this line.look into this code and
please help me out.

TabStop[] tabStop=new TabStop[1];
tabStop[0]=new TabStop();
tabStop[0].Position=5001;
tabStop[0].Alignment=TabAlign.CENTER;


XTextCursor xTextCursor = (XTextCursor)UnoRuntime.queryInterface(XTextCursor.class,tabStop[0]);

XText HeaderText = (XText) UnoRuntime.queryInterface(XText.class, xStyleProps.getPropertyValue("HeaderText"));
HeaderText.insertString(xTextCursor, s1, true);
OpenOffice 4.3.1 on Windows 7
Post Reply