OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

Writer: Merging two documents into one

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets
View previous topic :: View next topic  
Author Message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Wed Feb 18, 2004 5:54 am    Post subject: Writer: Merging two documents into one Reply with quote

The question has been asked more than once, how to merge two Writer documents into one.

The answer is simple...
1. Open first document.
2. Select All
3. Copy
4. Close

5. Open new merge document.
6. Paste

7. Open second document.
8. Select All
9. Copy
10. Close

11. Select new merge document
12. Paste

Simple.

In fact, so simple a macro could do it.
Code:
Sub Main
   ' Create the output document.
   oMergedDoc = StarDesktop.loadComponentFromURL( "private:factory/swriter", "_blank", 0, Array() )
   
   ' Where are the input documents?
   cMergeFolder = "C:\Documents and Settings\dbrewer\Desktop\"
   cMergeFile1 = "doc1.sxw"
   cMergeFile2 = "doc2.sxw"
   
   ' Open first merge document, Select All, Copy, then close document.
   oInputDoc = StarDesktop.loadComponentFromURL( ConvertToURL( cMergeFolder + cMergeFile1 ), "_blank", 0, Array() )
   SelectAll( oInputDoc )
   ClipboardCopy( oInputDoc )
   oInputDoc.close( True )
   
   ' Paste everything copied from merge1 into combined document.
   ClipboardPaste( oMergedDoc )
   
   ' Open second merge document, Select All, Copy, then close document.
   oInputDoc = StarDesktop.loadComponentFromURL( ConvertToURL( cMergeFolder + cMergeFile2 ), "_blank", 0, Array() )
   SelectAll( oInputDoc )
   ClipboardCopy( oInputDoc )
   oInputDoc.close( True )
   
   ' Paste everything copied from merge2 into combined document.
   ClipboardPaste( oMergedDoc )
End Sub



Next, you may ask, "Where did you get those routines like ClipboardCopy() and SelectAll() ?"

Go to this message....
Making the Dispatcher easier to use
http://www.oooforum.org/forum/viewtopic.php?t=5058
Grab the following subroutines....
ClipboardPaste()
ClipboardCopy()
ClipboardCut()
SelectAll() (this is further down the thread)
In order for these four to work, you also need to grab...
DocumentDispatch()
In order for DocumentDispatch() to work you need...

Go to this article....
Document model, controller and frame
http://www.oooforum.org/forum/viewtopic.php?t=5057
and grab this routine....
GetDocumentFrame()

The first thread does contain the SelectAll() routine, it is just further down the thread...
http://www.oooforum.org/forum/viewtopic.php?p=22352#22352
_________________
Want to make OOo Drawings like the colored flower design to the left?


Last edited by DannyB on Thu Apr 01, 2004 6:53 am; edited 2 times in total
Back to top
View user's profile Send private message
8daysaweek.co.uk
Super User
Super User


Joined: 29 Nov 2003
Posts: 2130
Location: UK

PostPosted: Wed Feb 18, 2004 5:59 am    Post subject: Reply with quote

Is this the same as:
http://www.ooomacros.org/user.php#108803

Or is there some different behaviour between the two?
_________________
James
www.8daysaweek.co.uk - A User-Focused OOo site
Back to top
View user's profile Send private message Visit poster's website AIM Address
avantman42
Super User
Super User


Joined: 28 Jul 2003
Posts: 751
Location: Staffordshire, UK

PostPosted: Wed Feb 18, 2004 6:06 am    Post subject: Reply with quote

8daysaweek.co.uk wrote:
Is this the same as:
http://www.ooomacros.org/user.php#108803

Or is there some different behaviour between the two?


They're different. Danny's method copies/pastes via the clipboard, whereas my Combine Files macro uses the TextCursor's InsertDocumentFromURL method, which is analogous to selecting Insert -> File from Writer's menu.

Russ
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Wed Feb 18, 2004 6:11 am    Post subject: Reply with quote

Russ wrote:
They're different. Danny's method copies/pastes via the clipboard, whereas my Combine Files macro uses the TextCursor's InsertDocumentFromURL method, which is analogous to selecting Insert -> File from Writer's menu.

Russ, I had not seen yours yet. Interesting technique though. Much better to use the API (yours) than the dispatcher (mine).

The technique I posted above really has much more general applicability for people what would like to write simple routines such as...
1. Select spreadsheet cells A1:J27
2. Copy
3. Select sheet Fred of document2
4. Paste
You sunk my battleship!
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
tapia
Guest





PostPosted: Thu Apr 01, 2004 1:14 am    Post subject: Reply with quote

avantman42 wrote:
They're different. Danny's method copies/pastes via the clipboard, whereas my Combine Files macro uses the TextCursor's InsertDocumentFromURL method, which is analogous to selecting Insert -> File from Writer's menu.


I've tried this method and it is almost OK, but I have a big problem: it inserts a page break before inserting the file. Is there any way for inserting it as it works with copy/paste, without the page break? It is very important in my project.

Thank you very much.

C.
Back to top
Gabor
Super User
Super User


Joined: 21 Sep 2003
Posts: 610
Location: Hungary (E-Europe)

PostPosted: Thu Apr 01, 2004 6:46 am    Post subject: Excuse me Reply with quote

From a very simple user's point of view neither of the two versions can be used easily.

Dannyb's version would anyway work with hard-coded folder and with hard-coded files only.

Avantman's version messes up the first few lines of the documents.

Of course this branch of the fourum is not for taking ready-to-use parts, I know that, it is for learning new solutions.
Back to top
View user's profile Send private message
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Thu Apr 01, 2004 7:56 am    Post subject: Re: Excuse me Reply with quote

Gabor wrote:
Dannyb's version would anyway work with hard-coded folder and with hard-coded files only.


In the example I hardcoded the pathnames to keep the example simple.

There is no reason that the pathnames cannot be determined dynamically. There is also no reason that the technique is limited to only merging two documents rather than many.

For example it would be possible to write a simple adaptation of the program which merges all writer documents from one folder into a single new document.

1. Create new merge document.
2. Loop for each file in a folder
2.1. Open next document in the folder
2.2. Select All
2.3. Copy
2.4. Close document
2.5. Paste into merge document from step 1.
2.6. Continue loop on next document in the folder, goto step 2.1.

In this example, the file names are not hardcoded, nor are we limited to merging only two documents. I just wanted to keep the example simple.

None of this of course answers the question of how to avoid the page breaks.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
Guest






PostPosted: Mon Apr 05, 2004 11:14 pm    Post subject: Reply with quote

Why not use InsertDocumentFromUrl to merge two documents?

1. Open your first document.
2. Set the Cursor to the position where you will the second docment merge in.
3. Load the second document:
Code:
oViewCursor.InsertDocumentFromUrl(oUrl,oLoadParams);
Back to top
DannyB
Moderator
Moderator


Joined: 02 Apr 2003
Posts: 3991
Location: Lawrence, Kansas, USA

PostPosted: Tue Apr 06, 2004 5:44 am    Post subject: Reply with quote

Anonymous wrote:
Why not use InsertDocumentFromUrl to merge two documents?


From several standpoints it is better to use InsertDocumentFromUrl. First of all, I would rather always use the API (InsertDocumentFromUrl) instead of the dispatcher (my macro copy-paste technique).

As tapia points out above, using InsertDocumentFromUrl inserts a page break, which may be undesirable. It may be possible to overcome this by first having two cursors, one that you insert into, and one that stays put at the point where the insert occurs. Then you could find the first newly inserted paragraph and remove the page break. As far as I know, nobody has posted such a technique yet.

I started this thread without thinking about InsertDocumentFromUrl because I had in hand my high-level routines such as
ClipboardCopy()
ClipboardCut()
ClipboardPaste()
SelectAll()
and it seemed trivial to write a copy-paste macro to merge two documents.
_________________
Want to make OOo Drawings like the colored flower design to the left?
Back to top
View user's profile Send private message
avantman42
Super User
Super User


Joined: 28 Jul 2003
Posts: 751
Location: Staffordshire, UK

PostPosted: Thu Apr 22, 2004 7:07 am    Post subject: Reply with quote

tapia wrote:
avantman42 wrote:
They're different. Danny's method copies/pastes via the clipboard, whereas my Combine Files macro uses the TextCursor's InsertDocumentFromURL method, which is analogous to selecting Insert -> File from Writer's menu.


I've tried this method and it is almost OK, but I have a big problem: it inserts a page break before inserting the file. Is there any way for inserting it as it works with copy/paste, without the page break? It is very important in my project.


I know it's been some time, but I've been busy on other things. I've just released version 1.1 of Combine Files, which allows the user to select whether or not to insert page breaks before the inserted documents:
http://www.ooomacros.org/user.php#108803

Russ
Back to top
View user's profile Send private message
Mr YouP
Newbie
Newbie


Joined: 08 Apr 2004
Posts: 4

PostPosted: Tue May 25, 2004 11:37 pm    Post subject: Reply with quote

I have trouble with InsertDocumentFromURL behavior. I want a page break but it doesn't put one for me :/
I get inspired by avantman42 macro but still don't understand.

here is the code :

Code:

set oViewCursor = objDocument.CurrentController.getViewCursor()
set oTextCursor = objDocument.Text.createTextCursorByRange(oViewCursor.getStart())
oTextCursor.InsertDocumentFromURL "file:///c|/rafale.sxw", Array()
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Code Snippets All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group