[Solved] Inserting Image in Frame

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
wmorrison1967
Posts: 4
Joined: Fri Dec 08, 2017 6:55 pm

[Solved] Inserting Image in Frame

Post by wmorrison1967 »

Hello,
I am trying to insert an image within a frame. This image is a picture of a signature. A specific text is placed in the frame where the signature will placed.
This is the VB Code:

Code: Select all

   Set oSearchDesc = oStarOfficeDoc.createSearchDescriptor  
    oSearchDesc.SearchString = sSignHere  <--This is the text that is located in the frame
    oSearchDesc.SearchWords = True
    Set oFound = oStarOfficeDoc.findFirst(oSearchDesc)
    Set oText = oStarOfficeDoc.GetText()

    //Setup the image for processing later
    Set oGraphicObject = oStarOfficeDoc.createInstance("com.sun.star.text.GraphicObject")
    sGraphicUrl = ConvertToUrl(sGraphicUrl)
    oGraphicObject.GraphicURL = sGraphicUrl
    oGraphicObject.Width = 6000
    oGraphicObject.Height = 2500
    oGraphicObject.AnchorType = 1
 
    Set oCursor = oText.createTextCursorByRange(oFound)  <--I am getting a error at this line    -->com.sun.star.uno.RuntimeException
    Call oText.insertTextContent(oCursor, oGraphicObject, False)
I am not sure why the creation of the cursor in the frame is causing an error.

Regards,
Will
Last edited by Hagar Delest on Sun Dec 10, 2017 11:11 pm, edited 3 times in total.
Reason: tagged [Solved].
OpenOffice 4.1 on windows 10
FJCC
Moderator
Posts: 9248
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Inserting Image in Frame

Post by FJCC »

I think the frame is not part of the Text of the oStarOfficeDoc. Try replacing this line

Code: Select all

oText = oStarOfficeDoc.GetText()
with

Code: Select all

oText = oFound.getText()
In that case, you may not need a cursor at all and can just use something like

Code: Select all

oText.insertTextContent(oText.Start, oGraphicObject, False)
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Inserting Image in Frame

Post by Lupp »

I'm not at all familiar with the kind of task, but:
Neither a TextFrame nor a GraphicObject inserted into the text of a document gets actually an element of that text. It is made an element of the DrawPage of the text document.
Therefore the text object ordered to insert the GraphicObject at the cursor position (which is inside the frame where the search string was found) must be the text object of the frame and not the text object of the document.
As the textcursor returned by the search loops back to that text, the following api call should do what the OQ wants:

Code: Select all

oCursor.Text.insertTextContent(oCursor, oGraphicObject, False)
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
wmorrison1967
Posts: 4
Joined: Fri Dec 08, 2017 6:55 pm

Re: Inserting Image in Frame

Post by wmorrison1967 »

Thanks guys. I combined both of the recommendations and it worked fine.

This is the code:

Code: Select all

Set oSearchDesc = oStarOfficeDoc.createSearchDescriptor
    oSearchDesc.SearchString = sSignHere
    oSearchDesc.SearchWords = True
    Set oFound = oStarOfficeDoc.findFirst(oSearchDesc)
    Set oText = oFound.GetText()      ' oStarOfficeDoc.GetText()
      
    
    Set oGraphicObject = oStarOfficeDoc.createInstance("com.sun.star.text.GraphicObject")
    sGraphicUrl = ConvertToUrl(sGraphicUrl)
    oGraphicObject.GraphicURL = sGraphicUrl
    oGraphicObject.Width = 6000
    oGraphicObject.Height = 2500
    oGraphicObject.AnchorType = 1    
    
    
    Set oCursor = oText.createTextCursorByRange(oFound)
    Call oText.insertTextContent(oCursor.Start, oGraphicObject, False)
I am going to post a new question in the forum about Frames
OpenOffice 4.1 on windows 10
User avatar
Lupp
Volunteer
Posts: 3542
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Inserting Image in Frame

Post by Lupp »

Since the result returned by the search is a TextCursor anyway, you may omit the additional oCursor object and also the special oText object (See my previous post!) and replace

Code: Select all

Set oCursor = oText.createTextCursorByRange(oFound)
Call oText.insertTextContent(oCursor.Start, oGraphicObject, False)
with

Code: Select all

Call oFound.Text.insertTextContent(oFound.Start, oGraphicObject, False)
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
Post Reply