Function to return properties of an object rather than their names

I know that UNO objects have the property DBG_properties, which returns a string with the names of the properties supported by that object. However, I want to access those properties, for which I would need the properties themselves, not only their names as strings.

Is it possible in Libreoffice Basic to get a list of the properties of an object rather than just their names?

For example, let’s suppose that I have a com.sun.star.beans.PropertyValue UNO structure and I want to retrieve its properties (Name and Value) without knowing the names of the properties. And if the property happens to be an object then I want to access its properties, too, and so on.

Thank you in advance.

Hi

The future version 5.1 will add a new service allowing easy access to the documentation.

Meanwhile there are tools. I personally use XRay. Its purpose is to display the properties, methods, services, interfaces which are provided by an object variable. X-Ray is a tool, originally developed for OpenOffice.org by Bernard Marcelly.

Regards

Hallo

In this context we should also mention about Mri -which seems have some comparative advantages versus Xray ( non-blocking execution, kind of Code generation for Basic, Python, Java, …; )

personally I “do” lot of Introspection interactive from ipython notebook sessions with the advantage of

someobject.<[tab]> ...  #produces complete method_tree of someobject 

more Details about that stuff … scroll down to my answer

I may not have been clear enough in my question but I wanted to access the properties of objects programatically. XRay and MRI are very useful tools but they don’t enable me to access the properties in my macro.

However, I found the solution in Andrew Pitonyak’s book: OpenOffice.org Macros Explained, Third Edition. Once we have the names of the properties of an object (e.g. using the property DBG_properties), we can access them in the following two ways:

  • If the object supports the com.sun.star.beans.XPropertySetInfo interface then we can retrieve the value of certain properties using the getPropertyValue(propertyName) method. However, not all of the properties are available through this interface, so this method sometimes produces an error.

  • In all cases, we can insert a new module into a library, containing a function that returns the value of the desired property.

My function returning the value of a given property can be seen below. In order to run properly, you will need a library called “TestLib” and it also has to be loaded (double click on its name).

Function myGetPropertyValue (oObj As Object, sPropName As String) As Variant
	Dim oPropInfo, oLib
	Dim sCode As String
	
	On Local Error GoTo NoProperty
		oPropInfo = oObj.getPropertySetInfo()
		If oPropInfo.hasPropertyByName (sPropName) Then
			myGetPropertyValue = oObj.getPropertyValue (sPropName)
			Exit Function
		EndIf
	NoProperty:
		oLib = GlobalScope.BasicLibraries.getByName("TestLib")
		sCode = "Option Explicit" & Chr$(10) & "Function getProp (oObj As Object)" & _ 
			Chr$(10) & _ "On Local Error GoTo propErr" & Chr$(10) & _ 
			"getProp = oObj." & sPropName & Chr$(10) & "Exit Function" & Chr$(10) & _ 
			"propErr:" & Chr$(10) & "getProp = Error" & Chr$(10) & "End Function"
		oLib.insertByName ("MyTest", sCode)
		myGetPropertyValue = getProp (oObj)
		oLib.removeByName("MyTest")
End Function
1 Like