1 我打开一个存储的文档,存储它,调用 MsgBox,关闭文档。 2 我打开一个新文档并再次调用 MsgBox 并获取对象没有属性 getCurrentController
我认为关闭第一个文档后,MsgBox 已附加到第一个文档,所以我将“_default”更改为“_blank”,然后我认为这是时间问题,所以我延迟了 3 秒。这是我的代码:
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
从新文档中获取控制器(如果它处于活动状态)。这是我使用的代码的示例。
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.")