Description: Tying to insert a linked graphicobject via ThisComponent.createInstance("com.sun.star.text.GraphicObject") and oText.insertTextContent(oCursor, oGraph, False) leads to an empty image frame (loading error) Steps to Reproduce: Customize the image url and run this Basic code: Sub InsertGraphicObject Dim oDoc Dim sURL Dim oCursor Dim oGraph Dim oText sURL= ConvertToUrl("C:\Users\username\image.jpg") oDoc = ThisComponent oText = oDoc.getText() oCursor = oText.createTextCursor() oCursor.gotoStart(False) oGraph = oDoc.createInstance("com.sun.star.text.GraphicObject") With oGraph .GraphicURL = sURL .Width = 6000 .Height = 8000 End With oText.insertTextContent(oCursor, oGraph, False) End Sub Actual Results: An empty image frame appears saying "Loading Error" Expected Results: The image should be inserted correctly and appear. Reproducible: Always User Profile Reset: No Additional Info:
Confirmed with Ubuntu 18.04 and Version: 6.1.1.0.0+ Build ID: 30c178dcb3301527ad92bbd245d1525ab77e314e The macro works as expected in 6.0.5 and 6.2Alpha
This could be related to a resolved issue from the changes to how images are handled. See: https://bugs.documentfoundation.org/show_bug.cgi?id=117427
Will add Tomaž Vajngerl to CC, perhaps he would know on that.
@Raal, is it the same case you investigated with Miklos in the Hamburg Hackfest?
Kai Struck, Could you please read the comments in bug 117427 to help you adapt your code?
Xisco Faulí, Ok, after reading it and using the function getGraphFromUrlAsLink for oGraph.Graphic I can make the above example code work: Sub InsertGraphicObject Dim oDoc Dim sURL Dim oCursor Dim oGraph Dim oText sURL= ConvertToUrl("http://pmg.pmgroup.be/enews/deroulard/exploit1.jpg") oDoc = ThisComponent oText = oDoc.getText() oCursor = oText.createTextCursor() oCursor.gotoStart(False) oGraph = oDoc.createInstance("com.sun.star.text.GraphicObject") oGraph.Graphic = getgraphfromurlaslink(sURL) With oGraph rem .GraphicURL = sURL .Width = 6000 .Height = 8000 End With oText.insertTextContent(oCursor, oGraph, False) End Sub function getGraphFromUrlAsLink(sFileUrl as String ) as Object oProvider = createUnoService("com.sun.star.graphic.GraphicProvider") Dim oPropsIN(1)as new com.sun.star.beans.PropertyValue oPropsIN(0).Name = "URL" oPropsIN(0).Value = converttoURL(sFileUrl) oPropsIN(1).Name = "LoadAsLink" oPropsIN(1).Value = TRUE getGraphFromUrlAsLink = oProvider.queryGraphic(oPropsIN()) end function To get the URL of the image we now must use oGraph.Graphic.OriginURL instead of oGraph.GraphicURL If that means that all older code has to be adapted (possibly with a version check to make it compatible with older versions) then I'm not too happy about that.
With simple version check for compatibility for older Versions (just for info, still hoping not being forced to use it): Sub InsertGraphicObject Dim oDoc Dim sURL Dim oCursor Dim oGraph Dim oText sURL= ConvertToUrl("http://pmg.pmgroup.be/enews/deroulard/exploit1.jpg") oDoc = ThisComponent oText = oDoc.getText() oCursor = oText.createTextCursor() oCursor.gotoStart(False) oGraph = oDoc.createInstance("com.sun.star.text.GraphicObject") if Officeversion="LibreOffice6.1" then oGraph.Graphic = getgraphfromurlaslink(sURL) else oGraph.GraphicURL = sURL end if With oGraph .Width = 6000 .Height = 8000 End With oText.insertTextContent(oCursor, oGraph, False) if Officeversion="LibreOffice6.1" then msgbox oGraph.Graphic.OriginURL else msgbox oGraph.GraphicURL end if End Sub function getGraphFromUrlAsLink(sFileUrl as String ) as Object oProvider = createUnoService("com.sun.star.graphic.GraphicProvider") Dim oPropsIN(1)as new com.sun.star.beans.PropertyValue oPropsIN(0).Name = "URL" oPropsIN(0).Value = converttoURL(sFileUrl) oPropsIN(1).Name = "LoadAsLink" oPropsIN(1).Value = TRUE getGraphFromUrlAsLink = oProvider.queryGraphic(oPropsIN()) end function function Officeversion GlobalScope.BasicLibraries.loadLibrary("Tools") Officeversion = GetProductName() end function
Hallo, will the api changes go into any version higher than 6.0.x.x? If so then I would use a version check like: if val(replace(Officeversion,"LibreOffice",""))>6 then oGraph.Graphic = getgraphfromurlaslink(sURL) else oGraph.GraphicURL = sURL end if function Officeversion If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then GlobalScope.BasicLibraries.LoadLibrary("Tools") Officeversion = GetProductName() end function
I have just tested the code from comment 6, as is, in Apache OpenOffice 3.4.1. And it just works. The API used there should be working down to OOo 3.3.0 (in 3.2.0, neither that, nor previous code would work). So - what is the reason for version checks suggested in comments 7 and 8?
Ah, sorry - not "as is"; just with uncommented ".GraphicURL = sURL" line, which also doesn't give problems here with LO 6.2.2.1 (and could be wrapped into On Error Resume Next ... On Error Goto 0).
@ Mike Kaganski The Code from Comment 6 only "seems" to work without problems in older LO versions and OO. But it doesn't link the image. the image will be embedded. At least on all my tests here. Therefore the version checks.
Ah, ok , forget my previous answer, I misread your comment 10. You are right! It should work for all versions (LO and OO) with this code: Sub InsertGraphicObject Dim oDoc Dim sURL Dim oCursor Dim oGraph Dim oText sURL= ConvertToUrl("http://pmg.pmgroup.be/enews/deroulard/exploit1.jpg") oDoc = ThisComponent oText = oDoc.getText() oCursor = oText.createTextCursor() oCursor.gotoStart(False) oGraph = oDoc.createInstance("com.sun.star.text.GraphicObject") oGraph.Graphic = getgraphfromurlaslink(sURL) With oGraph .GraphicURL = sURL .Width = 6000 .Height = 8000 End With oText.insertTextContent(oCursor, oGraph, False) End Sub function getGraphFromUrlAsLink(sFileUrl as String ) as Object oProvider = createUnoService("com.sun.star.graphic.GraphicProvider") Dim oPropsIN(1)as new com.sun.star.beans.PropertyValue oPropsIN(0).Name = "URL" oPropsIN(0).Value = converttoURL(sFileUrl) oPropsIN(1).Name = "LoadAsLink" oPropsIN(1).Value = TRUE getGraphFromUrlAsLink = oProvider.queryGraphic(oPropsIN()) end function