How to get target from LibreOffice WrappedTargetException?

62 views Asked by At

This is about automation of LO Base using Python macros.

Please see this question in the LO forum posed by me yesterday.

As you can see, from the link in my second post, it is trivial to open a form on the OpenDocument event, i.e. when the file is opened, if you use a VisualBasic macro.

However, attempts to open a form programmatically using Python macros always seem to lead to WrappedTargetException. e.g.:

def open_contacts_form(e):
    odb = e.Source
    container = odb.FormDocuments
    obj = container.getByHierarchicalName('kernel.contacts')
    obj.open() # causes the WrappedTargetException

But I can't find out how to access the initial (target) exception. I printed out (to a file) dir(e), and I don't see the attributes I expect to find from the API page for WrappedTargetException, such as TargetException, etc.

I have a suspicion unorthodox thread use could be causing the problem. But I don't understand how to dig into WrappedTargetException for greater enlightenment.

1

There are 1 answers

7
Jim K On

To dig into WrappedTargetException, catch the exception and use an introspection tool such as MRI.

import uno
from com.sun.star.lang import WrappedTargetException

def open_contacts_form(oEvent):
    odb = oEvent.Source
    container = odb.FormDocuments
    obj = container.getByHierarchicalName('kernel.contacts')
    try:
        obj.open()
    except WrappedTargetException as err:
        mri(err)

def mri(myObject):
    """Display a dialog to analyze UNO objects.
    The MRI extension is required.
    """
    ctx = XSCRIPTCONTEXT.getComponentContext()
    mri_obj = ctx.ServiceManager.createInstanceWithContext(
        "mytools.Mri", ctx)
    mri_obj.inspect(myObject)

(I did not test this code.)

For further information, including the link for downloading MRI, see Need some explanation about interface in LibreOffice basic.