problem invoking msgbox after closing one document in python. object has no attribute 'getCurrentController'

25 Views Asked by At

1 I open one stored document, store it, call MsgBox, close de document. 2 I open a new document and call MsgBox again and get object has no attribute getCurrentController

I thought that after closing the first document MsgBox was attached to the first document so I changed "_default" for "_blank' then I thought that it was a matter of time so I put a 3 second delay. This is my code:

import uno,os,time
from com.sun.star.beans import PropertyValue
def MsgBox(message):
    ctx = uno.getComponentContext()
    sm = ctx.getServiceManager()
    desktop = sm.createInstanceWithContext('com.sun.star.frame.Desktop', ctx)
    oDoc = desktop.getCurrentComponent().getCurrentController()
    oToolkit = sm.createInstance('com.sun.star.awt.Toolkit')
    msgbox = oToolkit.createMessageBox(oDoc.getFrame().getContainerWindow(),"infobox",1,"LibreOffice",str(message))
    msgbox.execute()
    msgbox.dispose()
    return
def documento_1():
    ctx = uno.getComponentContext()
    sm = ctx.getServiceManager()
    desktop = sm.createInstanceWithContext('com.sun.star.frame.Desktop',ctx)
    opciones =[]
    opcion = PropertyValue()
    opcion.Name = 'As Template'
    opcion.Value = False
    opciones.append(opcion)
    opcion.Name ='Password'
    opcion.Value = 'letmein'
    opciones.append(opcion)
    ruta = uno.systemPathToFileUrl('/home/jaguar/Documentos/Untitled 1.odt')
    #first document
    doc = desktop.loadComponentFromURL(ruta,'_blank',0,tuple(opciones))
    MsgBox(doc.hasLocation())

    doc.store()
    doc.close(True)
    time.sleep(3)
    #second new document
    doc1 = desktop.loadComponentFromURL('private:factory/swriter','_blank',0,())
    time.sleep(3)
    #Here is the problem
    MsgBox(doc1.hasLocation())
    doc1.close(True)
    return


1

There are 1 best solutions below

0
Jim K On

Get the controller from the new document if it's active. Here is an example based on code I use.

newDoc = self.mainDoc.desktop.loadComponentFromURL(
    "private:factory/swriter", "_blank", 0, ())
self.writerDoc = self.mainDoc.loadDocObjs(newDoc)

def loadDocObjs(self, newDocument=None, doctype=DOCTYPE_WRITER):
    """Load UNO objects from self.document into the current object."""
    self.document = newDocument
    if newDocument is None:
        # Get whatever has the active focus.
        # This is not always reliable on Linux when opening and closing
        # documents because of focus rules, so hang on to a reference to
        # the document when possible.
        self.document = self.desktop.getCurrentComponent()
    try:
        # This will fail if either the document was not obtained (a simple
        # NoneType error) or if the document was disposed.
        self.controller = self.document.getCurrentController()
    except AttributeError:
        raise AttributeError("Could not get document.")