OpenOffice’s Dispatch API seems like a great big hack and it is something one tries to avoid using when working with the API but
sometimes the API just won’t do what one would like it do i.e. Copying and Pasting content.
The Dispatch API allows developers to execute slots where there is some function in the office but it is not exposed by the API.
In the code below, the slot “.uno:Copy” is being executed, which is the same functionality in the UI as copy (CTRL-C).
executeSlot( ctx, controller, ".uno:Copy" )
def executeSlot( ctx, controller, islot ):
dispatchHelper = ctx.ServiceManager.createInstanceWithContext( \
"com.sun.star.frame.DispatchHelper", ctx )
frame = controller.getFrame()
dispatchHelper.executeDispatch( frame, islot, "", 0, () )
A nicer way to do copy/paste if the API was capable of it, would be:
textEnum = doc1.Text.createEnumeration()
textCursor = doc2.Text.createTextCursor()
para = textEnum.nextElement()
textCursor.insertRange( textCursor, para, absorb )
textCursor.gotoEnd( False )
In the first example, if one wanted to copy and paste content between documents, they are reliant on the operating system’s clipboard
In the second example, the code is only reliant on OpenOffice but the there is no insertRange() function on textCursors.
Which is a nice solution do you think?