I am trying to replace an existing image (a external image) with another one but I can´t modify the image property Graphic.OriginURL
Ok @rodri , show the code (or better, a document with the code and an example image) - let’s try to figure out which of the lines of the macro needs to be corrected.
Sub macrorReplaceImage
Dim oDoct As Object, docProperties()
Dim sUrl, sImageFile, sImageURL As String
Dim oExistingGraph As Object
sUrl = convertToURL(“C:\Varios\Libtest\Doc1.odt”)
sImageFile = “C:\Varios\Libtest\italy.png”
sImageURL = convertToURL(sImageFile)
if fileExists(sUrl) then
oDoct = stardesktop.LoadComponentFromURL(sUrl, “_blank”,0, docProperties())
else
msgbox “Not found”
end if
oDP = oDoct.getGraphicObjects()
oExistingGraph = oDP.getbyIndex(0)
Print oExistingGraph.Graphic.OriginURL
oExistingGraph.Graphic.OriginURL = sImageURL
End Sub
Doc1.odt (9.4 KB)
The image´s url is correctly read but it fails at the last line because OriginURL is read-only
Change last line in your macro:
oExistingGraph.GraphicURL = sImageURL
That solved it, thans
Hello KamilLanda and rodri,
I tried your example and the Graphic is changed but,
the Graphic is not linked it is embedded.
How do I set it as linked?
Best Regards
Wolfgang
Hello Wolfgang, I discovered the insertion only via the macro from Macro Recorder.
But I suppose the best solution could be to detect the properties of replaced image (like Anchor Type and Vertical Orientation), and set these properties to new image - but this detection isn’t in example.
Sub insertImageAsLink
dim oDoc as object, document as object, dispatcher as object, sUrl$, oSel as object, oSize as new com.sun.star.awt.Size
sUrl=ConvertToUrl("d:\opti.png") 'your image
oDoc=ThisComponent
document=oDoc.CurrentController.Frame
dispatcher=createUnoService("com.sun.star.frame.DispatchHelper")
dim args1(3) as new com.sun.star.beans.PropertyValue
args1(0).Name="FileName"
args1(0).Value=sUrl
args1(1).Name="FilterName"
args1(1).Value="<All images>"
args1(2).Name="AsLink"
args1(2).Value=true
args1(3).Name="Style"
args1(3).Value="Graphics"
dispatcher.executeDispatch(document, ".uno:InsertGraphic", "", 0, args1() )
rem inserted image is selected, so you you can change the properties of one
oSel=oDoc.CurrentController.Selection 'inserted image
with oSel
.AnchorType=com.sun.star.text.TextContentAnchorType.AS_CHARACTER
.VertOrient=com.sun.star.text.VertOrientation.CHAR_CENTER
end with
oSize=oSel.ActualSize
with oSize 'simulate the proper DPI of inserted image
.Width=2/3*.Width
.Height=2/3*.Height
end with
oSel.Size=oSize
End Sub
If you will have the problem with replacement, upload some example ODT for testing.
(post deleted by author)