Macro to Insert Image in Writer - Illegal Argument Error on DrawPages().Add

Hello. I have been working with macros within LO for a few months now. I am having trouble getting part of this macro to work. I have implemented an edited version of Pitonyak’s image insert macro from his guide. I get the same error running that as I do running an unedited example from an old LO Basic Guide (v3). The error I get is “Type: com.sun.star.lang.IllegalArgumentException” when trying to pass a “com.sun.star.drawing.GraphicObjectShape” Object to a draw page via Doc.DrawPages().Add(GraphicObjectShape).

I’m probably missing something very simple and basic. I am on LO Writer version 6.4.7.2. I have tried doing this in LO Draw and I get the same error.
Here is the macro from the reference book-
Dim Doc As Object
Dim Page As Object
Dim GraphicObjectShape As Object
Dim Point As New com.sun.star.awt.Point
Dim Size As New com.sun.star.awt.Size

Point.x = 1000         ' specifications, insignificant because latter
                       coordinates are binding
Point.y = 1000
Size.Width = 10000
Size.Height = 10000

Doc = ThisComponent
Page = Doc.DrawPages(0)

GraphicObjectShape = Doc.createInstance("com.sun.star.drawing.GraphicObjectShape")

GraphicObjectShape.Size = Size
GraphicObjectShape.Position = Point
 
GraphicObjectShape.GraphicURL = "file:///c:/test.jpg"
GraphicObjectShape.AdjustBlue = -50
GraphicObjectShape.AdjustGreen = 5
GraphicObjectShape.AdjustBlue = 10
GraphicObjectShape.AdjustContrast = 20
GraphicObjectShape.AdjustLuminance = 50
GraphicObjectShape.Transparency = 40
GraphicObjectShape.GraphicColorMode = com.sun.star.drawing.ColorMode.STANDARD

Page.add(GraphicObjectShape)

And here is the adjusted macro from Pitonyak’s reference book

Sub InsertSigImage(oDoc, oCurs, sURL$)
	Dim oShape 
	Dim oGraph 
	Dim oProvider
	Dim oText
	Dim Test as String

    oShape = oDoc.createInstance("com.sun.star.drawing.GraphicObjectShape")
    oGraph = oDoc.createInstance("com.sun.star.text.GraphicObject")
    oDoc.getDrawPage().add(oShape)
	
	oProvider = createUnoService("com.sun.star.graphic.GraphicProvider")
	
	Dim oProps(0) as new com.sun.star.beans.PropertyValue
	oProps(0).Name = "URL"
	oProps(0).Value = sURL
	
	REM Save original size
	Dim oSize100thMM
	Dim lHeight as Long
	Dim lWidth as Long
	oSize100thMM = RecommendGraphSize(oProvider.queryGraphicDescriptor(oProps))
	If NOT IsNull(oSize100thMM) AND NOT IsEmpty(oSize100thMM) Then
		lheight = oSize100thMM.Height
		lWidth = oSize100thMM.Width
	EndIf
	
	oShape.Graphic = oProvider.queryGraphic(oProps())
	oGraph.graphic = oShape.graphic
	oGraph.AnchorType = com.sun.star.text.TextContentAnchorType.AS_CHARACTER
	oText = oCurs.getText()
	oText.insertTextContent(oCurs, oGraph, false)

	oDoc.getDrawPage().remove(oShape)
	
	If lHeight > 0 AND lWidth > 0 Then
		Dim oSize
		oSize = oGraph.Size
		oSize.Height = lHeight
		oSize.Width = lWidth
		oGraph.Size = oSize
	End If
	
End Sub


Function RecommendGraphSize(oGraph)
	Dim oSize
	Dim lMaxW As Double
	Dim lMaxH As Double
	
	lMaxW = 6.75 * 2540
	lMaxH = 9.5 & 2540
	
	If isNull(oGraph) or IsEmpty(oGraph) Then
		Exit Function
	End IF
	
	oSize = oGraph.Size100thMM
	If oSize.Height = 0 OR oSize.Width = 0 Then
		oSize.Height = oGraph.SizePixel.Height * 2540.0 * TwipsPerPixelY() / 1440
		oSize.Width = oGraph.SizePixel.Width * 2540.0 * TwipsPerPixelX() / 1440
	EndIf
	If oSize.Height = 0 OR oSize.Width = 0 Then
		Exit Function
	End If
	If oSize.Width > lMaxW Then
		oSize.Height = oSizeHeight * lMax / oSize.Width
		oSize.Width = lMaxW
	End If
	If oSize.Height > lMaxH Then
		oSize.Width = oSize.Width * lMaxH / oSize.Height
		oSize.Height = lMaxH
	EndIf
	RecommendGraphSize = oSize
End Function

Thank you for reading

your change code is not right, the example is quite good to follow. the reason why such problem aiaes is that the GraphicURL isn’t a leagl path, causing graph is a NULL object.

You can look this example:

https://wiki.documentfoundation.org/Macros/Writer/ba002