After tough fight and lots of experimentation I figured out how to copy a selection from one document to another, copy everything - tables, images, text style, everything preserved. Not like that dummy example in wiki where only bare text is copied over.
It works nearly as desired, only one exception - the original document does not return to original "view", it hangs at the very bottom of the document, on the last page. I can return view cursor back to original position, that works, but the view does not follow the view cursor. Pressing like arrow keys brings back view cursor back into active view, back into focus.
You need at least two page document to notice the issue.
Code: Select all
'''
''' Copy content from one document to another document;
''' select all and copy, open new blank unnamed document and paste all the selected content from first document
''' Copy everything, images, tables, text styles; rich text copy
'''
Sub CopyContentToNewFile
dim c1, c2, newDoc, selectedContent, origPosition, origPositionTC
origDocument = ThisComponent
' preserve original position, when working with controller selection otherwise it gets lost
origPosition = origDocument.CurrentController.getViewCursor()
origPositionTC = origDocument.getText.createTextCursorByRange(origPosition)
' select all in original document
c1 = origDocument.text.createTextCursor
c1.gotoStart(false)
c1.gotoEnd(true)
' copy selected from original document
origDocument.CurrentController.select(c1) ' select content of the whole document, from the start to the end; here cursor represents whole text range, selected content of a document
selectedContent = origDocument.CurrentController.getTransferable() ' copy the content for later use
' create new blank document
newDoc = StarDesktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, Array()) 'Open blank Writer doc
' paste selected to the new document, on cursor position
c2 = newDoc.text.createTextCursor '
c2.gotoStart(false)
newDoc.CurrentController.select(c2) ' set insertion point, represented by cursor C2, in the new file at the document start; here cursor represents pointer to a position, not a range
newDoc.CurrentController.insertTransferable(selectedContent)
' deselect current selection; current selection is whole document by now and it is visible selection
' return cursor to original position; and selection to original selection if any was present before running macro
origDocument.CurrentController.select(origPositionTC)
' ..problem is, although cursor returns to the original position Writer view is stuck on the last page.
End Sub
Macro is run from main menu "Macro -> CopyContentToNewFile"