[Solved] Copy one document(text,images) into other

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
sumukh17
Posts: 11
Joined: Thu Oct 28, 2010 1:26 pm

[Solved] Copy one document(text,images) into other

Post by sumukh17 »

Hello there,
I need copy text/tables/images from one word document to a specific location in other word document. I am not able to find out way to select all text/images (sort of Ctrl+A) and paste it to other document. I am using OpenOffice API.

Thanks in anticipation

Sumukh
Last edited by Hagar Delest on Mon Nov 08, 2010 10:25 pm, edited 1 time in total.
Reason: tagged [Solved].
OpenOffice 3.2
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Copy one document(text,images) into other

Post by rudolfo »

I hope you mean writer document and not word document!
With "text/table/images" do you mean all content? Your comparison to Ctrl-A makes me think so ...
Apart from the OpenOffice API what scripting/programming language do you use?

If you want to copy everything (this includes two flavours: "all content" or "all content with all styles/formatting") you may look for something that does the same as the menu command "Insert -> File"
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
sumukh17
Posts: 11
Joined: Thu Oct 28, 2010 1:26 pm

Re: Copy one document(text,images) into other

Post by sumukh17 »

Hi,
Thanks for the reply. Actually I have a MS word template(having bookmarks etc..) and I am generating a new document based on this template. All other source documents are in MS word format.
I am using OOo API for reading the template and replacing bookmarks with proper content.
Yes, I want to copy text/images etc from the source document to the target document(at specified location/bookmark).
I want to do this programatically since we need to generate lots of documents based on template.
As of now I am not using any scripting language, but I am open to use any scripting language for this.

Please advise.

Thanks
Sumukh
OpenOffice 3.2
sumukh17
Posts: 11
Joined: Thu Oct 28, 2010 1:26 pm

Re: Copy one document(text,images) into other

Post by sumukh17 »

Just to add,
I already have managed to replace word bookmarks with text. What I am looking for is to replace a bookmark with set of text as well as images from other document.

Thanks
Sumukh
OpenOffice 3.2
User avatar
Hagar Delest
Moderator
Posts: 32665
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: Copy one document(text,images) into other

Post by Hagar Delest »

Do you want to continue working in .doc format? Remember that until recently, that specification was not public. The import/export filters have been reverse engineered. So by manipulating such imported MS Word items, you may face some problems. Better redo the templates from scratch IMHO. Checking the documents made from such a macro will also take time.

Using OOo with .doc format may lead to some trouble, especially if there are tables in the documents. Can't you switch to ODF?
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Copy one document(text,images) into other

Post by rudolfo »

Sumukh, there is something in what you write that I can't bring together in my mind: All your documents are in MS Word format (the templates and also the documents that should be merged into the themplate). So why do you want to use OpenOffice? Automation is a big thing in MS Word as well.
And I don't see where and how you use the OpenOffice API? You have only said how you don't use it: with scripting languages. To help you we need to know what you are doing. Apart from the scripting languages (Basic, Python, Beanshell, Javascript) there are languages that are commonly labeled as programming languages: C++, C Sharp, Java
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
sumukh17
Posts: 11
Joined: Thu Oct 28, 2010 1:26 pm

Re: Copy one document(text,images) into other

Post by sumukh17 »

Hello Hagar/Rudolfo,
I agree with you. We do plan to migrate to ODF. We can create templates from scratch, that should not be an issue at all. I am just trying to finish up the component development, once it is done, I can migrate to ODF.

Rudolfo,
My bad, I did not mention that I am using Java API.
I need to write a Java component, which will read the document template and replace the bookmarks with appropriate content. The content will come from other documents and it might contain text/images/tables etc. So far, using OOo Java API, I have managed to replace a bookmark either with text or image from other document.
Here is what I am looking for
I want to replace a bookmark with entire contents of other document (text/images etc. ). I am not able to find out how do I read entire document and paste it in other document ? I know there is something like uno:selectall() uno:paste(), but I am not sure how to use it .

I hope I am clear now.

Thanks
Sumukh
OpenOffice 3.2
sumukh17
Posts: 11
Joined: Thu Oct 28, 2010 1:26 pm

Re: Copy one document(text,images) into other

Post by sumukh17 »

Hello,
I have now converted MS word templates to ODF templates.
I still need to select all/copy/paste entire document contents into the other using Java API or some kind of script.
Any help would really be appreciated.

Thanks
Sumukh
OpenOffice 3.2
User avatar
Hagar Delest
Moderator
Posts: 32665
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: Copy one document(text,images) into other

Post by Hagar Delest »

Have you checked the documentation listed in the first sticky of this forum section? See especially Andrew Pitonyak's document, it contains a lot of code snippets.
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Copy one document(text,images) into other

Post by rudolfo »

uno:selectall() and uno:paste() are so called dispatcher calls. This is the way the macro recorder works, but not the peferred one. It is easier on first sight because you don't have to dig your way through the UNO API documenation, but it has ome limitations (performance, concurrent use of the clipboard,..) You might have a look at the following thread Java, XDispatchHelper: can't dispatch a ".uno:Paste" that describes a problem with OOo 3.2.
Also pick up some of the key words there and try running a search on the macro forum. As far as I remember I have seen several postings that were talking about document processing with Java.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
sumukh17
Posts: 11
Joined: Thu Oct 28, 2010 1:26 pm

Re: Copy one document(text,images) into other

Post by sumukh17 »

Hagar/Rudolfo,
Thanks for the reply.
I am going through Andrew Pitonyak's document. I am exploring if I can call OOo Basic macro from a Java Class.

Rudolfo,
What would you recommend to use instead of dispatcher (due to the problems you had mentioned..) ?
I got to know about Transferable interface which does not use clipboard.

Thanks again for all your help.

Sumukh
OpenOffice 3.2
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Copy one document(text,images) into other

Post by rudolfo »

sumukh17 wrote:exploring if I can call OOo Basic macro from a Java Class
No, don't do this. The Basic macros use the same UNO objects and Interfaces as the Java classes. Typical Basic macro code can be described with the following pseudo code:
  • Get an object reference to the current document
  • list all paragraphs of the document
  • for each paragraph do some modification to the paragraph text
  • enumerate all bookmarks in the document
  • create a Text-Cursor at the n-th bookmark
  • insert some text or an image at the cursor
It's all about creating UNO objects or traversing through enumerations of UNO objects and calling the methods of these objects to apply changes. That's 80% of the code is UNO related and maybe 20% percent basic.
Actually the code for the different languages is so similar that an extension (the MRI) can generate Code for 5 different languages (Basic, Python, Java, Javascrip, Rexx). I really recommend to download this extension. It helps you a lot to explore the object hierachy of OpenOffice (and it has a online help integrated into OpenOffice's F1 key -- Read it!).
There is one caveat with this tool: The code window is initially hidden (shrunk to zero height). You need to place the mouse over the border between the "status bar" and the window with the properties.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
sumukh17
Posts: 11
Joined: Thu Oct 28, 2010 1:26 pm

Re: Copy one document(text,images) into other

Post by sumukh17 »

Hello Rudolfo,
Thanks for the pseudo code.

1. * Get an object reference to the current document
2. * list all paragraphs of the document
3. * for each paragraph do some modification to the paragraph text
4. * enumerate all bookmarks in the document
5. * create a Text-Cursor at the n-th bookmark
6. * insert some text or an image at the cursor

I have already managed to finish UNO/Java code till step 5. For step 6, I am either able to insert(replace bookmark) text the image from the document but not the both. And this is where I am stuck for last couple of days.
I need to find out a way to select entire document contents(image/text etc.) and then paste it in other document in place of bookmark.

Any pointers on this would be helpful

Thanks

Sumukh
OpenOffice 3.2
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Copy one document(text,images) into other

Post by rudolfo »

I come back to one of the previous questions that I brought up earlier: "copying all contents" includes two flavours: "all content" or "all content with all styles/formatting".
if I hear the word template I think of some pre-defined layout that will be filled with content. But mostly the format definitions come from the template and the inserted file content does only provide the semantic data.
Have you thought about Master Documents? They would achieve something like this. But instead of bookmarks you would use links to files. The style/formatting definitions in the Master overwrite those from the linked files. Once you are done you can export the .odm master document to a single .odt document with the file contents inside of sections.

Well, the answer for this question depends a bit on what kind of formatting is used in the files that should be inserted. If it is direct formatting (typically from the toolbar menu) then the styles in the master document wouldn't be applied. This style concept works only for indirect formatting based on styles that are managed by the Stylist (F11 key).

Frankly I won't be able to help you with the coding. I use scripting in Calc, but not in Writer (in my opinion most things that were done with Writer UNO objects can be done with the XML files and XSLT "directly" -- that's the beauty of open formats).
And actually in your templating case a loud voice in my mind says: "This is calling for XSLT".
But as you have invested quite some time in your approach I can only recommend you to search in Pitonyak's document or search again through this forum or in www.oooforum.org. As I said the languag of the sample code that you will find is not important. It can be converted easily to other programming languages. The crucial point is finding the appropriate UNO objects and methods.
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
FJCC
Moderator
Posts: 9279
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Copy one document(text,images) into other

Post by FJCC »

I'm not sure I am following all of this disucssion, but here is some simple Basic code to insert both the entire text and a picture from one document into another document.

Code: Select all

Sub Main
oText = ThisComponent.Text
Mark = ThisComponent.BookMarks.getByName("Mark1")
Curs = oText.createTextCursor
Curs.gotoRange(Mark.Anchor, False)
DocToInsert = "C:\Documents and Settings\username\My Documents\DocForInsert.doc"
DocToInsertURL = convertToURL(DocToInsert)
Curs.insertDocumentFromURL(DocToInsertURL,Array())
End Sub
I only tested this with a very simple document containing two paragraphs and a picture.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
rudolfo
Volunteer
Posts: 1488
Joined: Wed Mar 19, 2008 11:34 am
Location: Germany

Re: Copy one document(text,images) into other

Post by rudolfo »

Thanks FJCC, that's far less complicated as I would have expected. That's worth choosing a macro in favor of XSLT.
Hopefully Sumukh does a search for insertDocumentFromURL.
Once you know the keyword that you have to use there is quite a lot of information in this forum ;-)
OpenOffice 3.1.1 (2.4.3 until October 2009) and LibreOffice 3.3.2 on Windows 2000, AOO 3.4.1 on Windows 7
There are several macro languages in OOo, but none of them is called Visual Basic or VB(A)! Please call it OOo Basic, Star Basic or simply Basic.
sumukh17
Posts: 11
Joined: Thu Oct 28, 2010 1:26 pm

Re: Copy one document(text,images) into other

Post by sumukh17 »

Hello FJCC and Rudolfo.
I had came across insertDocumentFromURL keyword last week, thanks to http://www.oooforum.org/forum/viewtopic.phtml?t=6052
I even tried implementing it in java, but could not make it work. I will give it a shot one more time and will keep you updated regarding the progress.

Thanks for your help.
Sumukh
OpenOffice 3.2
sumukh17
Posts: 11
Joined: Thu Oct 28, 2010 1:26 pm

Re: Copy one document(text,images) into other

Post by sumukh17 »

Hello FJCC/Rudolfo et al,
Atlast.. I was able to replace a bookmark with text/graphics from other document.
insertDocumentFromURL() did work.. :)

Thanks for all your help

Sumukh
OpenOffice 3.2
Post Reply