Get different properties of two objects in LibreOffice Basic

asked 2016-02-19 12:17:24 +0200

Gabor2 gravatar image

updated 2016-03-08 21:47:23 +0200

Alex Kemp gravatar image

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)
edit retag flag offensive close merge delete