Macro which emulates dragging image to a page in ODG file

Good Afternoon,
I have a blank odg file. I add content by dragging a file like a jpg or PDF files (single page) from my folder to a blank page in the odg file. I have tried multiple macros to add images to pages of the odg file, but the image does not appear. I would appreciate any help. Thank you.

Regards

Let’s open a new Draw document with our website logo at its original size.

In the example, replace value of props(0).Value with the URL of your graphic file.

Sub AddGraphicShape()
  Dim oDoc As Object, oPage As Object, oShape As Object
  Dim oGraphic As Object, oDisp As Object
  Dim props(0) As New com.sun.star.beans.PropertyValue
  oDoc = StarDesktop.LoadComponentFromUrl("private:factory/sdraw", "_default", 0, Array())
  oPage = oDoc.DrawPages.getByIndex(0)
  oShape = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape")
  
  props(0).Name = "URL"
  props(0).Value = "https://ask.libreoffice.org/uploads/asklibo/original/1X/1eec1ce28d4605f25e751aea59dbef2bc0782151.png"
    
  oShape.Graphic = CreateUnoService("com.sun.star.graphic.GraphicProvider").queryGraphic(props)
  oPage.add(oShape)
  ' To original size
  oDisp = createUnoService("com.sun.star.frame.DispatchHelper")
  oDoc.CurrentController.Select oDoc.DrawPages.getByIndex(0).getByIndex(0)  ' select shape
  oDisp.executeDispatch oDoc.CurrentController.Frame, ".uno:OriginalSize", "", 0, Array()
End Sub

Your code works. Here is my code:

Sub QuickDragImage(sImagePath As String)
    ' Use this when you know the image path already
    Dim oDoc As Object
    Dim oPage As Object
    Dim oGraphicShape As Object
    oDoc = ThisComponent
    oPage = oDoc.getDrawPages().getByIndex(0)
    oGraphicShape = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape")
    oGraphicShape.Size.Width = 6000
    oGraphicShape.Size.Height = 6000
    oGraphicShape.Position.X = 2500
    oGraphicShape.Position.Y = 2500
    oGraphicShape.GraphicURL = ConvertToURL(sImagePath)
    oPage.add(oGraphicShape)
    ThisComponent.CurrentController.select(oGraphicShape)
    MsgBox "Image quickly dropped: " & sImagePath, 64, "Success"
End Sub

Any ideas where my code does not actually display/embed the image in the page?

Your code shows everything, but it’s impossible to see without a microscope. :slight_smile:
The code doesn’t take into account the specifics of working with structures in LibreOffice.
We could write it like this, for example (after defining the oSize and oPosition variables):

    oSize = oGraphicShape.Size
    oSize.Width = 6000
    oSize.Height = 6000
    oGraphicShape.Size = oSize
    
    oPosition = oGraphicShape.Position
    oPosition.X = 2500
    oPosition.Y = 2500
    oGraphicShape.Position = oPosition

The specifics of working with structures are discussed in detail in the analysis of the “Listing 197. Properties copy by value” in A.Pitonyak’s excellent book OOME_4_1.odt.

Navigation to Quickly Reach Objects
Development Tools