Merge multiple Writer odt files from Windows command line

Discuss the word processor
Post Reply
dculp
Posts: 32
Joined: Fri Apr 25, 2008 1:43 pm

Merge multiple Writer odt files from Windows command line

Post by dculp »

I need to do something like --
merge file1.odt file2.odt file3.odt > Result_file.odt

If needed I could do this 2 files at a time instead of 3 files at once.

This could be done from a Windows command line but I actually need to run from within a batch file.

I know that the files can be merged within the GUI but that's not what I need.

Version: 7.0.6.2 (x64)
Build ID: 144abb84a525d8e30c9dbbefa69cbbf2d8d4ae3b
CPU threads: 4; OS: Windows 10.0 Build 19045; UI render: Skia/Raster; VCL: win
Locale: en-US (en_US); UI: en-US
Calc: CL

Windows 10

Thanks,
Don C.
User avatar
robleyd
Moderator
Posts: 5383
Joined: Mon Aug 19, 2013 3:47 am
Location: Murbko, Australia

Re: Merge multiple Writer odt files from Windows command line

Post by robleyd »

This really old topic has a couple of links to sites with macros that might do what you want; I haven't tried them.

To save you a moment or two, Run macro from command line
Slackware 15 64 bit
Apache OpenOffice 4.1.15
LibreOffice 25.2.4.3; SlackBuild for 25.2.4 by Eric Hameleers
---------------------
Roses are Red, Violets are Blue]
Unexpected '{' on line 32
.
JeJe
Volunteer
Posts: 3068
Joined: Wed Mar 09, 2016 2:40 pm

Re: Merge multiple Writer odt files from Windows command line

Post by JeJe »

Zizi64's solution in the first search result to run a macro from a batch works for me.

viewtopic.php?p=385356#p385356
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
dculp
Posts: 32
Joined: Fri Apr 25, 2008 1:43 pm

Re: Merge multiple Writer odt files from Windows command line

Post by dculp »

Note that file1.odt file2.odt file3.odt and Result_file are not fixed files. The next time I run a merge it might be --

merge file7.odt file8.odt file9.odt > Result_file_new.odt

Thus, I need a general purpose method of merging odt files.

I could code a macro to take my specified files (example below to merge 2 files; adjust on-the-fly for different file names) and save it as Merge1.bas. Then how can I run Merge1.bas from the command line so that the currently opened Writer odt file is affected?

(The final resolution to my inquiry must be simple to implement. Otherwise, I could just use the GUI to "\Insert\Text from file ..." and find each desired odt file from the resulting long list.)

Code: Select all

REM  *****  BASIC  *****

Sub Main

End Sub

sub Merge_files
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
rem dispatcher.executeDispatch(document, ".uno:InsertDoc", "", 0, Array())

rem ----------------------------------------------------------------------
dim args2(1) as new com.sun.star.beans.PropertyValue
args2(0).Name = "Name"
args2(0).Value = "file:///F:MyFile1.odt"
args2(1).Name = "Filter"
args2(1).Value = "Text"

dispatcher.executeDispatch(document, ".uno:InsertDoc", "", 0, args2())

rem ----------------------------------------------------------------------
rem dispatcher.executeDispatch(document, ".uno:InsertDoc", "", 0, Array())

rem ----------------------------------------------------------------------
dim args4(1) as new com.sun.star.beans.PropertyValue
args4(0).Name = "Name"
args4(0).Value = "file:///F:MyFile2.odt"
args4(1).Name = "Filter"
args4(1).Value = "Text"

dispatcher.executeDispatch(document, ".uno:InsertDoc", "", 0, args4())

end sub
JeJe
Volunteer
Posts: 3068
Joined: Wed Mar 09, 2016 2:40 pm

Re: Merge multiple Writer odt files from Windows command line

Post by JeJe »

dculp wrote: Wed Feb 19, 2025 1:46 pm Then how can I run Merge1.bas from the command line so that the currently opened Writer odt file is affected?
Don't follow what you mean by that - you decide which frame is used when calling the dispatch helper.

If you always have the files in the same folder, then you can run any number of files you place there with the same code (containing that folder url)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
dculp
Posts: 32
Joined: Fri Apr 25, 2008 1:43 pm

Re: Merge multiple Writer odt files from Windows command line

Post by dculp »

@JeJe -
I code in Pascal so I don't really know anything about the code above which I just copied from a macro that I recorded in Writer. My intent was to write a small Pascal app that would be invoked like --

Merge_odt_files.exe file7.odt file8.odt file9.odt Merge_file_macro

Then Merge_odt_files.exe would take the 3 input files (file7.odt file8.odt file9.odt) and spit out the macro code into Merge_file_macro (basically creating the above macro). (I'm not sure what this macro should be called). Then I would run Merge_file_macro from the command line if this is possible.

1. Could Merge_file_macro (or the above macro code) be run from the command line?
2. If so, can you give an example of how would it be called?
JeJe
Volunteer
Posts: 3068
Joined: Wed Mar 09, 2016 2:40 pm

Re: Merge multiple Writer odt files from Windows command line

Post by JeJe »

Have you considered automation?

https://wiki.freepascal.org/Office_Automation
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
dculp
Posts: 32
Joined: Fri Apr 25, 2008 1:43 pm

Re: Merge multiple Writer odt files from Windows command line

Post by dculp »

That might be a possibility. I compiled this as LibreOffice_10a.exe on first try and then ran -
LibreOffice_10a Test1.odt
where Test1.odt is an existing file.
This opened Test1 in Writer with a name Untitled 2. (I'm surprised that it didn't adopt the Test1.odt name.)

I then tried it with 2 existing odt files but it only opened the first -
LibreOffice_10a Test1.odt Test1.odt

So right now this isn't helpful in merging the 2 test odt files. Maybe I'm missing something?
JeJe
Volunteer
Posts: 3068
Joined: Wed Mar 09, 2016 2:40 pm

Re: Merge multiple Writer odt files from Windows command line

Post by JeJe »

Try moving from the dispatch helper to a textcursor, then you can control where the document is inserted with it. In Basic:

Code: Select all


txt = thiscomponent.text
tc= txt.createtextcursorbyrange(txt.end) 'create a textcursor at the end of the document

tc.insertDocumentFromURL url1,array() '  ( [in] string aURL, [in] [].beans.PropertyValue aOptions ) 

tc.gotorange(txt.getend,false) 'move the cursor to the end of the document again or you'll overwrite what was just inserted

tc.insertDocumentFromURL url2,array()
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
dculp
Posts: 32
Joined: Fri Apr 25, 2008 1:43 pm

Re: Merge multiple Writer odt files from Windows command line

Post by dculp »

JeJe -
I have written an app in Pascal that allows merging any combination or number of odt and text files into Writer. I'm currently debugging - should release in about a week if no significant problems.
Post Reply