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

Creating a macro - Writing a Script - Using the API

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

Postby rudolfo » Wed Jan 13, 2010 9:12 pm

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   Expand viewCollapse view
   ..     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   Expand viewCollapse view
  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.
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

Postby hanya » Wed Jan 13, 2010 9:31 pm

I made little modification:
Code: Select all   Expand viewCollapse view
  ' 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
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

Postby rudolfo » Wed Jan 13, 2010 9:45 pm

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   Expand viewCollapse view
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.
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

Postby hanya » Wed Jan 13, 2010 10:05 pm

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
hanya
Volunteer
 
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan


Return to Macros and UNO API

Who is online

Users browsing this forum: eeigor and 8 guests