It is based on:
Code: Select all
cursorNewDoc.string = cursorOrigDoc.string
My solution still uses cursors, text cursors only, but to make copy it uses rather lesser know feature called 'transferables':
Code: Select all
selectedContent = origDocument.CurrentController.getTransferable()
newDoc.CurrentController.insertTransferable(selectedContent)
Code: Select all
'''
''' Copy content from one document to another document; from one file to another file.
''' select all and copy, open new blank unnamed document and paste all the selected content from first document
''' Copy everything, images, tables, text styles, embedded objects; rich text copy
'''
Sub CopyContentToNewFile
dim c1, c2, newDoc, selectedContent, origPosition, origPositionTC
origDocument = ThisComponent
origDocument.lockControllers()
' 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)
' set view cursor in the new document at the beginning of document
' not essential, feels more 'intuitive' behavior
newDoc.currentController.viewCursor.gotoStart(false)
origDocument.unlockControllers()
End Sub
Extra credit to FJCC for helping me with one last issue with my code.