Feed data into document from the command line

I have a document where I need to put data in a certain place. Is there any way to do it from the command line (soffice)? I.e., set a place in the document and then send the data to that place; create a variable where I can send the necessary data.

Maybe this can be done somehow with a macro. I’m not sure that’s even possible.

You could connect your document to a database and insert fields of the connected database to your document (Insert → Field → more Fields → Database).

You could also add content to template document by placeholders (Insert → Field → More Fields → Functions → Placeholder). This will only work with macro-code. It is described a little bit in Base Guide.

This can be done.

The text of the macro can be something like this:

Option Explicit 

Sub ReplacePlaceholders(Optional pairDelimiter As String, _
		 Optional partDelimiter As String, _
		 Optional task As String)
Const TAG_OPEN = "["
Const TAG_CLOSE = "]"
Dim oReplaceDescriptor As Variant
Dim aTasks As Variant
Dim aPair As Variant 
Dim i As Long, count As Long
	If Not IsMissing(aTasks) Then 
		aTasks = Split(task, partDelimiter)
		oReplaceDescriptor = ThisComponent.createReplaceDescriptor()
		count = 0
		For i = LBound(aTasks) To UBound(aTasks)
			aPair = Split(aTasks(i), pairDelimiter)
			If UBound(aPair) = 1 Then
				oReplaceDescriptor.setSearchString(TAG_OPEN & aPair(0) & TAG_CLOSE)
				oReplaceDescriptor.setReplaceString(aPair(1))
				count = count + ThisComponent.replaceAll(oReplaceDescriptor)
			EndIf 
		Next i
		If count = 0 Then
			MsgBox("No replacement tags found", MB_ICONEXCLAMATION, "Parameters may be incorrect")
		Else 
			MsgBox("Replaced " & count & " tags", MB_ICONINFORMATION, "Done")
		EndIf 
	EndIf 
End Sub

Place it in My Macros in the Standard library in a separate module (e.g. rplcOnOpen)

Insert placeholders (tags) in the necessary places in the document, mark them, for example, with square brackets.

Command which will done the job will be
"C:\Program Files\LibreOffice\program\soffice.exe" C:\Tests\Test_doc.odt "macro:///Standard.rplcOnOpen.ReplacePlaceholders(;,|,Name;Mad Max|Date;2022-01-01)"

The first part is the path to the LibreOffice executable file

The next part is the path to the document being processed - Test_doc.odt

The hardest to write part of the command is the third. Here are keyword macro:///, then Library.Module.Macro and in parentheses parameters for the macro.

3 Likes