How to write Basic sub or function with shape as output parameter

I have several subroutines, which start with accessing a selected shape. I want to move that part to an separate, common sub or function, so that I can use e.g.

dim oShape as variant
getSelectedShape(oShape)

Inside such sub I get the shape, but when back from getSelectedShape the parameter oShape is null. How to do it correctly?

I have attached an example document which contains such macro library. For testing select the Fontwork shape and then use the macros from module Main.
FontworkWithMacroLibrary.odp (18.0 KB)

It works for me like this:

Sub Test
  Dim oShape As Object, b As Boolean
  b=GetSelectedShape(oShape)
End Sub

I try to avoid Out and InOut parameters in Basic macros by using the ByVal option. To return more than one value in a function, you can use the construct:

MyFunc=Array(outValue1, outValue2, outValue3)  ' ...

In my opinion, it is more “reliable” to write your function like this:

Rem returns Nothing, if no selection at all or selection is not a shape or document type is not supported'
Function getSelectedShape2() as Object
  Dim oShape As Object 
  getSelectedShape2=Nothing
'  ---

The recommendations described above will allow, among other things, to avoid problems with calling a macro through the invoke method (both from Basic and from other programming languages in LO).

1 Like

Hi sokol92, that works. The essential part is to use Dim As Object and not Dim As Variant.

1 Like