[Solved] Adding tables to Writer using Basic

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
matt_leeds_1
Posts: 27
Joined: Thu Dec 31, 2009 1:22 pm

[Solved] Adding tables to Writer using Basic

Post by matt_leeds_1 »

Hi there

Could someone point me in the right direction for adding tables into writer row by row

I have found this so far which adds a single table at the current cursor

Code: Select all

  'Get view cursor 
  vCursor = ThisComponent.CurrentController.getViewCursor () 
  'Create a table 
  oTable = ThisComponent.createInstance ("com.sun.star.text.TextTable") 
  'Initialise table to have one row, three columns 
  oTable.initialize (1, 3)
  oTable.setPropertyValue "BackTransparent", false
  oTable.setPropertyValue "BackColor", 13421823    
  'Insert table at current cursor position 
  ThisComponent.Text.insertTextContent (vCursor, oTable, False)
I would like to be able to add a table of 1 row, X amount columns at a bookmark position and then add data row by row as its read from a csv file (from my previous post)

(I am also replacing text in writer with basic if anyone needs example of this i have a example)

Thanks
Last edited by matt_leeds_1 on Mon Jan 11, 2010 4:35 pm, edited 2 times in total.
OpenOffice 3.1 on Windows XP
matt_leeds_1
Posts: 27
Joined: Thu Dec 31, 2009 1:22 pm

Re: Adding tables to writer using Basic

Post by matt_leeds_1 »

I have found answer to one of my questions - adding a table at a bookmark

Code: Select all

  oTable = ThisComponent.createInstance( "com.sun.star.text.TextTable" )
  oTable.initialize(2, 3) 'Two rows, three columns
  oInsertPoint = ThisComponent.getBookmarks().getByName("BOOKMARK").getAnchor()
  oInsertPoint.getText().insertTextContent(oInsertPoint , oTable, False)
So just need now add a row one at a time to table.

Thanks
OpenOffice 3.1 on Windows XP
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Adding tables to Writer using Basic

Post by rudolfo »

You need to access the row or column enumeration and then you can insert (or append) additional rows or columns (see API documentation). In your example where you insert the table at a bookmark you simply need to append another line:

Code: Select all

 oTable.Rows.insertByIndex(1,2)
This inserts two rows between the first and the second row. As always indexes are starting at zero.

I only did a quick test, so I can't say for sure if the insertByIndex method is also working before you included the table into the document with the insertTextContent method. And actually I don't see why you would need this at an earlier stage. Because in that case you could simply initialize the table with the right number of rows.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
matt_leeds_1
Posts: 27
Joined: Thu Dec 31, 2009 1:22 pm

Re: Adding tables to Writer using Basic

Post by matt_leeds_1 »

Hi Rudolfo.

Thanks for the reply.

Individual table lengths are not known until instruction from csv file tells it to start new table. Is there a way to always append to table based on number of existing rows rather than use constants (1,2)

Thanks
OpenOffice 3.1 on Windows XP
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Adding tables to Writer using Basic

Post by rudolfo »

Of course you can use variables instead of the literal values in insertByIndex.
I am not really sure what you really want to do. Now as you are talking about csv files it sounds like you have an existing table in a Writer document and you want to append the contents of the csv file to this table.
In that case I feel not really comfortable with the current approach because insertByIndex adds only empty rows and you would need to fill each cell in another loop with:

Code: Select all

oTable.getCellByPosition( nCol, nRow ).setString("the Text content of the cell")
Maybe you'd better load/import the csv file into Calc and use OLE linking to embed the Calc table/sheet into the Writer document.
If you still want to achieve this with a macro for Writer you might want to check out the following information: It should help you with the typical tasks like finding the tables in a document, retrieve the number of rows in a table, etc.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
matt_leeds_1
Posts: 27
Joined: Thu Dec 31, 2009 1:22 pm

Re: Adding tables to Writer using Basic

Post by matt_leeds_1 »

Thanks that has helped get me going again

Final code is this

Code: Select all

          'insert one row at end of table
          oTable.Rows.insertByIndex(oTable.getRows().getCount(),1)
        
          BomFields = split(aLine,",")
          
          'fill last row
          for index = 0 to numColumns - 1
            oTable.getCellByPosition(index, oTable.getRows().getCount()-1).setString(Trim(BomFields(index))) 
          next 
OpenOffice 3.1 on Windows XP
Post Reply