[Basic] Creating new text in writer

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Pontifex
Posts: 12
Joined: Wed Sep 17, 2008 4:45 am

[Basic] Creating new text in writer

Post by Pontifex »

I want to insert and manipulate some text into a new writer document from a macro:
  • I want to be able to insert text into a new document I just created with a macro.
    I want to bold, underline and apply italics to this text.
    I want to be able to create bulleted text.
    I want to be able to center the text.
    I want to be able to insert tabs between portions of text
I checked the documentation and there's nothing on the subject of inserting text into a document, centering text, bulleting text and tabs. There is some talk of adjusting the bold / italics / underline, but no examples. There's a guide for developers but the XML is broken, so I can't look at it.

Some examples or more extensive documentation would be appreciated.

Thank you.

--Pontifex
OOo 2.4.X on Ms Windows XP + Linux (Ubuntu)
Pontifex
Posts: 12
Joined: Wed Sep 17, 2008 4:45 am

Re: [Basic] Creating new text in writer

Post by Pontifex »

The code:

Code: Select all

document = StarDesktop.loadComponentFromURL(document_type, document_frame, 0, document_properties())
Always creates a new GUI writer document, no matter if I use:

Code: Select all

document_frame = "_hidden"
Or

Code: Select all

document_frame = "_blank"
So I'd either like to know what the real setting is to create a new invisible document or the method to close the newly opened document.

--Pontifex
OOo 2.4.X on Ms Windows XP + Linux (Ubuntu)
DukeOfURL
Posts: 5
Joined: Wed Oct 01, 2008 5:05 pm

Re: [Basic] Creating new text in writer

Post by DukeOfURL »

Pontifex wrote:I want to insert and manipulate some text into a new writer document from a macro:
  • I want to be able to insert text into a new document I just created with a macro.
    I want to bold, underline and apply italics to this text.
    I want to be able to create bulleted text.
    I want to be able to center the text.
    I want to be able to insert tabs between portions of text
I checked the documentation and there's nothing on the subject of inserting text into a document, centering text, bulleting text and tabs. There is some talk of adjusting the bold / italics / underline, but no examples. There's a guide for developers but the XML is broken, so I can't look at it.

Some examples or more extensive documentation would be appreciated.

Thank you.

--Pontifex
I'm going to second this request. I can figure out pretty easily from the documentation how to connect to a data source in Basic, what I'd like to do is take the data I read in through that source and insert it into my document, formatted. "Mail merge" doesn't work because I don't want to create a separate document or page for each row, but rather a single, continuous document that has each row of the database (say, Headline, Category, Summary, Benefits, Costs) formatted one after the other, such as:

Headline1 [Category1]
Summary1
Benefits: Benefits1
Costs: Costs1

Headline2 [Category2]
Summary2
Benefits: Benefits2
Costs: Costs2

...

Ideally, I'd also like to know how to create a table in much the same basic manner.

FWIW, I'm using OO 3.0 on both Mac OS X (10.5) and Windows XP (SP3).
OOo 2.4.X on Mac OSx Leopard + Windows XP
DukeOfURL
Posts: 5
Joined: Wed Oct 01, 2008 5:05 pm

Re: [Basic] Creating new text in writer

Post by DukeOfURL »

Okay... answering my own question somewhat. Using the wonderful guide to macros by Andrew Pitonyak, I derived a few methods to help out:

Code: Select all

Sub WriteString( data as String, txt as Object, cursor as Object, bold as Boolean, underline as Boolean, italics as Boolean )
	SetStyle( cursor, bold, underline, italics )
	txt.insertString( cursor, data, FALSE )
End Sub

Sub Endl( txt as Object, cursor as Object )
	txt.insertControlCharacter( cursor, com.sun.star.text.ControlCharacter.LINE_BREAK, FLASE )
End Sub

Sub Endp( txt as Object, cursor as Object )
	txt.insertControlCharacter( cursor, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, FLASE )
End Sub

Sub SetStyle( cursor as Object, bold as Boolean, underline as Boolean, italics as Boolean )
	if bold then
		cursor.CharWeight = com.sun.star.awt.FontWeight.BOLD
	else
		cursor.CharWeight = com.sun.star.awt.FontWeight.NORMAL
	end if
	
	if underline then
		cursor.CharUnderline = com.sun.star.awt.FontUnderline.SINGLE
	else
		cursor.CharUnderline = com.sun.star.awt.FontUnderline.NONE
	end if	
	
	
	if italics then
		cursor.CharPosture = com.sun.star.awt.FontSlant.ITALIC
	else
		cursor.CharPosture = com.sun.star.awt.FontSlant.NONE
	end if		
End Sub
Mind you, this is for very simple formatting only, your mileage may vary. :)

Then, to invoke said code, I set up the document parameter, connect to the database, and loop:

Code: Select all

Sub PrintGuide
	Dim oDoc As Object
	Dim oText As Object
	Dim oVCurs As Object
	Dim oTCurs As Object
          
	oDoc = ThisComponent
	oText = oDoc.Text
	oVCurs = oDoc.CurrentController.getViewCursor()
	oTCurs = oText.createTextCursorByRange(oVCurs.getStart())
	
    ' Create a row-set to query the database
    RowSet = createUnoService("com.sun.star.sdb.RowSet")
    RowSet.DataSourceName = "CostBenefitAnalysis"
    RowSet.CommandType = com.sun.star.sdb.CommandType.COMMAND
    RowSet.Command = "SELECT Name, Category, Description, Benefits, Costs FROM Summary"
    RowSet.execute()
    
    ' loop through the resultset until no record is left
    while RowSet.next() 
		WriteString( rowSet.getString(1), oText, oTCurs, TRUE, TRUE, FALSE )      
		WriteString( "[" + rowSet.getString(2) + "]", oText, oTCurs, TRUE, FALSE, FALSE )     
		Endl( oText, oTCurs )
		
		WriteString( rowSet.getString(3), oText, oTCurs, FALSE, FALSE, TRUE )
		Endl( oText, oTCurs )

		if ( rowSet.getString(4) <> "" ) then
			WriteString( "Benefits: ", oText, oTCurs, TRUE, FALSE, FALSE )
			WriteString( rowSet.getString(4), oText, oTCurs, FALSE, FALSE, FALSE )
			Endl( oText, oTCurs )
		end if
		
		if ( rowSet.getString(5) <> "" ) then
			WriteString( "Costs: ", oText, oTCurs, TRUE, FALSE, FALSE )
			WriteString( rowSet.getString(5), oText, oTCurs, FALSE, FALSE, FALSE )
			Endl( oText, oTCurs )
		end if
			
		Endp( oText, oTCurs )								
    wend
End Sub
The subroutine, when invoked, connects to the database and renders the information into my Writer document just as I expect.
OOo 2.4.X on Mac OSx Leopard + Windows XP
Post Reply