[Basic] Copy full content from one document to another

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
espinosa_cz
Posts: 10
Joined: Thu Aug 20, 2015 4:15 am

[Basic] Copy full content from one document to another

Post by espinosa_cz »

I found solution to copy content between documents suggested in https://wiki.openoffice.org/wiki/Writer/API/Text_cursor highly inadequate.
It is based on:

Code: Select all

cursorNewDoc.string = cursorOrigDoc.string
This way only raw text is transferred. Who needs that!? And there is 64K limit, so forget about copying larger files at once this way.

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)
This copies everything: text styles, colors, backgrounds, highlightings, images, text tables and even embedded Calc sheets! All tested.

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
More info: viewtopic.php?f=20&t=78759
Extra credit to FJCC for helping me with one last issue with my code.
Attachments
CopyContent4.odt
(29.06 KiB) Downloaded 635 times
Last edited by espinosa_cz on Sun Aug 23, 2015 7:47 pm, edited 1 time in total.
Libre Office 4.4 / AOO 4.1 / Windows 8 & OpenSuse
User avatar
RoryOF
Moderator
Posts: 34611
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Basic] Copy full content from one document to another

Post by RoryOF »

If you wish to copy entire documents, Insertion of the entire document from a disc image is a better way. One can do this on a one off basis without a macro by /Insert /File.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
espinosa_cz
Posts: 10
Joined: Thu Aug 20, 2015 4:15 am

Re: [Basic] Copy full content from one document to another

Post by espinosa_cz »

My aim was was to do it intentionally by a macro to give a kick start example for more complex cases.

Potential usage is limitless. One example is to rewrite JohnV example from OO wiki, where pages of original document are saved separately in their own documents.

In my case, I have a contractors time sheet template, a Writer document with an embedded Calc table, witch generates a monthly pre-filled time sheet for me for given month.
I wanted to extend my template script to create a copy of that template, to a new document, opened, where I could review it, alter it if needed and save.
The output document has to be stripped of all macros from template.

I searched hard, but I could not find a way how to copy a template (or any document) to a new document; where I could put some potential modifications, like stripping macros; a code which can be part of my larger automated workflow. It took me a lot of time, countless, experiments, several late going to bed, so to same others the pain, I am sharing it.
Libre Office 4.4 / AOO 4.1 / Windows 8 & OpenSuse
espinosa_cz
Posts: 10
Joined: Thu Aug 20, 2015 4:15 am

Re: [Basic] Copy full content from one document to another

Post by espinosa_cz »

Sadly, I have just find an issue. My macro does not work well for documents starting with a table, see details:
viewtopic.php?f=20&t=79065
If the table is at the very start of the document, the very first element, then the table is ignored, copying starts from first text, or embedded sheet (surprisingly).
You can bypass this issue by by ensuring that there is some text in front of the fist table in the document; it can be invisible or barely visible text, like by 1px size font.
Libre Office 4.4 / AOO 4.1 / Windows 8 & OpenSuse
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Basic] Copy full content from one document to another

Post by Villeroy »

What is the difference between this macro and Ctrl+A, Ctrl+C, Ctrl+N, Ctrl+V ?
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
espinosa_cz
Posts: 10
Joined: Thu Aug 20, 2015 4:15 am

Re: [Basic] Copy full content from one document to another

Post by espinosa_cz »

Because if it programmatic it can be part of more complex workflows.
Like here: https://bitbucket.org/espinosa/contract ... tgenerator
I have just outsourced my Contractor Timesheet Generator!
Libre Office 4.4 / AOO 4.1 / Windows 8 & OpenSuse
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Basic] Copy full content from one document to another

Post by Villeroy »

Well, that looks like a database report. A similar thing could be done without a single line of code.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply