Ask Your Question

Gabor2's profile - activity

2017-09-30 15:01:39 +0200 received badge  Student (source)
2016-06-11 17:27:28 +0200 received badge  Famous Question (source)
2016-03-03 16:49:02 +0200 received badge  Popular Question (source)
2016-03-03 16:49:02 +0200 received badge  Notable Question (source)
2016-03-01 17:17:12 +0200 answered a question Get different properties of two objects in LibreOffice Basic

For some reason this question doesn't show up in Google search. Does it have to get answered to show up? We'll see...

2016-02-24 15:30:07 +0200 asked a question Get different properties of two objects in LibreOffice Basic

The documentation of MRI mentions a useful tool called diff: if you can change a certain property through the GUI but the macro recorder doesn't record this change then you can still find out the name of the changed property by using diff, which points you to the property that has been changed and that you need to use.

However, it requires python UNO bridge for LibreOffice, which may be a bit awkward to set up. For those who don't want to bother themselves with python (like me), it may be more convenient to have such a diff macro in LibreOffice Basic. Therefore, I wrote such a macro, which I want to share here in the hope that it may be useful to other people, as well. The entire macro is based on Andrew Pitonyak's book: OpenOffice.org Macros Explained, Third Edition.

How to use the macro

Create two instances (Obj1 and Obj2) of the same object of type Type and modify the properties in question in only one of them. Then call Diff_obj (Obj1, Obj2, Type, 0). After a sufficient run a message box will show a list of the different properties of the two objects. In the code below I compared to charts (with different data ranges).

The macro contains recursive function calls because some properties are objects themselves, having other properties, which again may be objects with properties and so on. However, some of these "chains" seem to be infinite, making it necessary to limit the steps made in the object hierarchy, which is done by introducing maxDepth.

I wrote comments in the code. For some more information on the function myGetPropertyValue please refer to Function to return properties of an object rather than their names.

In order to run the macro, you will need a library called "TestLib" and it also has to be loaded (double click on its name).

I am using Ubuntu 14.04 and LibreOffice 4.2.8.2.

Option Explicit

Dim oLib
Dim maxDepth As Integer

Sub myDiff
    Dim chart_1, chart_2 As Object

    oLib = GlobalScope.BasicLibraries.getByName("TestLib")
    maxDepth = 5

    chart_1 = ThisComponent.Sheets(0).Charts(0).EmbeddedObject
    chart_2 = ThisComponent.Sheets(0).Charts(1).EmbeddedObject

    MsgBox Diff_obj (chart_1, chart_2, "EmbeddedObject", 0), 0, "Different properties"
End Sub

Function IsWhiteSpace (iChar As Integer) As Boolean
    Select Case iChar
    Case 9, 10, 13, 32, 160 ' The character is tab, CR, LF, space or a non-breaking space.
        IsWhiteSpace = True
    Case Else
        IsWhiteSpace = False
    End Select
End Function

Function TrimWhite (s As String) As String ' Remove white spaces from both the front and the end of a string
    s = Trim(s)
    Do While Len(s) > 0
        If Not IsWhiteSpace(ASC(s)) Then Exit Do
        s = Right(s, Len(s) - 1)
    Loop
    Do While Len(s) > 0
        If Not IsWhiteSpace(ASC(Right(s, 1))) Then Exit Do
        s = Left(s, Len(s) - 1)
    Loop
    TrimWhite = s
End Function

Sub showMsg (dialMsg As String, Optional dialTitle As String)
    If IsMissing (dialTitle) Then
        If MsgBox (dialMsg ...
(more)
2016-02-24 15:27:38 +0200 answered a question Function to return properties of an object rather than their names

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
2016-02-19 18:16:50 +0200 received badge  Famous Question (source)
2016-02-07 10:46:11 +0200 received badge  Notable Question (source)
2016-02-06 03:03:41 +0200 received badge  Popular Question (source)
2016-02-05 14:46:49 +0200 asked a question 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.