[Solved] Writer macro to insert an image into a table cell

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

[Solved] Writer macro to insert an image into a table cell

Post by rudolfo »

I am trying to create an "autotext" for a notice paragraph with an icon on the left and the note text indented.

Code: Select all

   ..     This is the content of
  (  )    the paragraph which might take
   \/     more lines and exceed the height
          of the icon on the left.
I made several attempts with a simple paragraph and background images or anchored images where the text contents is wrapped around. ... and I gave up. I decided that this can only be accomplished with a one row, two column table. I tried to put all the steps into a macro, so that I can insert this with one mouse click.
But I am stuck when trying to insert a graphic into a table cell. Inserting an image into a normal paragraph is working (I used code from Andrew Pitonyak Macro book).

Code: Select all

  oDoc = ThisComponent
  ' Create a table and attach/insert it at the current cursor position
  vViewCursor = oDoc.getCurrentController().getViewCursor()
  oTable = oDoc.createInstance("com.sun.star.text.TextTable")
  oTable.initialize(1, 2)
  oDoc.getText.insertTextContent( vViewCursor, oTable, False )

  ' I can manipulate the table (Text content of the right cell, borders ...)

  ' Create an Image with the method from Andrew's book
  oImage = oDoc.createInstance("com.sun.star.text.GraphicObject")
  With oImage
     .GraphicURL = ConvertToURL("C:\temp\note.png")
     .AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
     .Width = 3000
     .Height = 3000
  End With

  ' I get quite a lot of exceptions when I inspect the graphic object with Xray
  ' but that's like that in normal paragraphs and the graphic is inserted without problem

  ' create a text cursor in the left cell
  oCursor = oTable.getCellByPosition(0,0).createTextCursor()
  ' Xray shows that the created cursor has all the typical cursor properties and methods

  oDoc.getText.insertTextContent( oCursor, oImage, False )   ' RUNTIME ERROR HERE
For the last line with the insertTextContent I get a runtime error with the message "text interface and cursor not related".
Probably because the table is not part of the normal ThisComponent.getText stream. Seems like I need a "text something" from the table cell for that I can call the insertTextContent method.
The API says about the XText or XSimpleText interface that defines this insert method:
is the main interface for a distinct text unit, i.e. the main text of a document, the text for headers and footers or for single cells of a table.
Clearly there is TEXT in a table cell! But how do I get access to this object?

oTable.getCellByPosition(0,0).iWantTheText().insertTextContent(oCursor,oImage,False) ??

Any ideas? Thank you ...
Last edited by rudolfo on Fri Jan 15, 2010 2:22 am, edited 2 times in total.
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.
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Writer macro for inserting an image into a table cell

Post by hanya »

I made little modification:

Code: Select all

  ' create a text cursor in the left cell
  oCell = oTable.getCellByPosition(0,0)'.createTextCursor()
  ' Xray shows that the created cursor has all the typical cursor properties and methods

  oCell.getText.insertTextContent( oCell, oImage, False )   ' RUNTIME ERROR 
Use correct css.text.XText interface for the table. The interface of the document and the table are not the same.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Writer macro for inserting an image into a table cell

Post by rudolfo »

In the meantime I have figured out what this iWantTheText() needs to be: simply nothing.
Xray showed me that the table cell itself has an insertTextContent method.

Code: Select all

 oTable.getCellByPosition(0,0).insertTextContent(oCursor,oImage,False)
works for me.
But your method is really neat. I thought that the first parameter of insertTextContent must always be a cursor like object. But right, a table cell itself is good enough.
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.
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Writer macro for inserting an image into a table cell

Post by hanya »

If you use the X-ray, see interfaces. First arguments of insertTextContent method is always css.text.XTextRange interface. Interface is not clear if you use Basic or other script bridge implemented using css.script.XInvocation interface, but working with C++ or Java bridge it is clear.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
Post Reply